152SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
352SN/A * All rights reserved.
452SN/A *
552SN/A * Redistribution and use in source and binary forms, with or without
652SN/A * modification, are permitted provided that the following conditions are
752SN/A * met: redistributions of source code must retain the above copyright
852SN/A * notice, this list of conditions and the following disclaimer;
952SN/A * redistributions in binary form must reproduce the above copyright
1052SN/A * notice, this list of conditions and the following disclaimer in the
1152SN/A * documentation and/or other materials provided with the distribution;
1252SN/A * neither the name of the copyright holders nor the names of its
1352SN/A * contributors may be used to endorse or promote products derived from
1452SN/A * this software without specific prior written permission.
1552SN/A *
1652SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1752SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1852SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1952SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2052SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2152SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2252SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2452SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2552SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2652SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
2952SN/A */
3052SN/A
31401SN/A#include <unistd.h>
3252SN/A
338655Sandreas.hansson@arm.com#ifdef __APPLE__
348655Sandreas.hansson@arm.com#include <mach/mach_init.h>
358655Sandreas.hansson@arm.com#include <mach/shared_region.h>
368655Sandreas.hansson@arm.com#include <mach/task.h>
3711793Sbrandon.potter@amd.com
388655Sandreas.hansson@arm.com#endif
398655Sandreas.hansson@arm.com
4011793Sbrandon.potter@amd.com#include "base/hostinfo.hh"
4111793Sbrandon.potter@amd.com
428229Snate@binkert.org#include <cctype>
438229Snate@binkert.org#include <cerrno>
448229Snate@binkert.org#include <cmath>
458229Snate@binkert.org#include <cstdio>
46401SN/A#include <cstdlib>
47401SN/A#include <cstring>
48401SN/A#include <string>
49401SN/A
5012334Sgabeblack@google.com#include "base/logging.hh"
519142Ssteve.reinhardt@amd.com#include "base/str.hh"
526214Snate@binkert.org#include "base/types.hh"
5352SN/A
54401SN/Ausing namespace std;
55401SN/A
56401SN/Astring
57401SN/A__get_hostname()
58401SN/A{
59401SN/A    char host[256];
60401SN/A    if (gethostname(host, sizeof host) == -1)
61401SN/A        warn("could not get host name!");
62401SN/A    return host;
63401SN/A}
64401SN/A
65401SN/Astring &
66401SN/Ahostname()
67401SN/A{
68401SN/A    static string hostname = __get_hostname();
69401SN/A    return hostname;
70401SN/A}
71401SN/A
7252SN/Auint64_t
735202Sstever@gmail.comprocInfo(const char *filename, const char *target)
7452SN/A{
7552SN/A    int  done = 0;
7652SN/A    char line[80];
7752SN/A    char format[80];
7892SN/A    long usage;
7952SN/A
8052SN/A    FILE *fp = fopen(filename, "r");
8152SN/A
8252SN/A    while (fp && !feof(fp) && !done) {
8352SN/A        if (fgets(line, 80, fp)) {
849142Ssteve.reinhardt@amd.com            if (startswith(line, target)) {
851875SN/A                snprintf(format, sizeof(format), "%s %%ld", target);
8652SN/A                sscanf(line, format, &usage);
8752SN/A
8852SN/A                fclose(fp);
8952SN/A                return usage ;
9052SN/A            }
9152SN/A        }
9252SN/A    }
9352SN/A
9452SN/A    if (fp)
958655Sandreas.hansson@arm.com        fclose(fp);
9652SN/A
9752SN/A    return 0;
9852SN/A}
998655Sandreas.hansson@arm.com
1008655Sandreas.hansson@arm.comuint64_t
1018655Sandreas.hansson@arm.commemUsage()
1028655Sandreas.hansson@arm.com{
1038655Sandreas.hansson@arm.com// For the Mach-based Darwin kernel, use the task_info of the self task
1048655Sandreas.hansson@arm.com#ifdef __APPLE__
1058655Sandreas.hansson@arm.com    struct task_basic_info t_info;
1068655Sandreas.hansson@arm.com    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
1078655Sandreas.hansson@arm.com
1088655Sandreas.hansson@arm.com    if (KERN_SUCCESS != task_info(mach_task_self(),
1098655Sandreas.hansson@arm.com                                  TASK_BASIC_INFO, (task_info_t)&t_info,
1108655Sandreas.hansson@arm.com                                  &t_info_count)) {
1118655Sandreas.hansson@arm.com        return 0;
1128655Sandreas.hansson@arm.com    }
1138655Sandreas.hansson@arm.com
1148655Sandreas.hansson@arm.com    // Mimic Darwin's implementation of top and subtract
1158655Sandreas.hansson@arm.com    // SHARED_REGION_SIZE from the tasks virtual size to account for the
1168655Sandreas.hansson@arm.com    // shared memory submap that is incorporated into every process.
1178655Sandreas.hansson@arm.com    return (t_info.virtual_size - SHARED_REGION_SIZE) / 1024;
1188655Sandreas.hansson@arm.com#else
1198655Sandreas.hansson@arm.com    // Linux implementation
1208655Sandreas.hansson@arm.com    return procInfo("/proc/self/status", "VmSize:");
1218655Sandreas.hansson@arm.com#endif
1228655Sandreas.hansson@arm.com}
123