hostinfo.cc revision 5202:ff56fa8c2091
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#include <ctype.h>
32#include <errno.h>
33#include <math.h>
34#include <unistd.h>
35
36#include <stdio.h>
37#include <cstdlib>
38#include <cstring>
39#include <string>
40
41#include "base/misc.hh"
42#include "sim/host.hh"
43
44using namespace std;
45
46string
47__get_hostname()
48{
49    char host[256];
50    if (gethostname(host, sizeof host) == -1)
51        warn("could not get host name!");
52    return host;
53}
54
55string &
56hostname()
57{
58    static string hostname = __get_hostname();
59    return hostname;
60}
61
62uint64_t
63procInfo(const char *filename, const char *target)
64{
65    int  done = 0;
66    char line[80];
67    char format[80];
68    long usage;
69
70    FILE *fp = fopen(filename, "r");
71
72    while (fp && !feof(fp) && !done) {
73        if (fgets(line, 80, fp)) {
74            if (strncmp(line, target, strlen(target)) == 0) {
75                snprintf(format, sizeof(format), "%s %%ld", target);
76                sscanf(line, format, &usage);
77
78                fclose(fp);
79                return usage ;
80            }
81        }
82    }
83
84    if (fp)
85      fclose(fp);
86
87    return 0;
88}
89