blob: 162716b75c4626a9beb3c0282e1b65aac2890cbb [file] [log] [blame]
Alexander Afanasyev41684ab2013-02-19 11:02:37 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef MEM_USAGE_H
22#define MEM_USAGE_H
23
24#ifdef __linux__
25#include <proc/readproc.h>
26#include <unistd.h>
27// #include <sys/resource.h>
28#endif
29#ifdef __APPLE__
30#include <mach/task.h>
31#include <mach/mach_traps.h>
32#include <mach/mach.h>
33#include <unistd.h>
34#include <err.h>
35#include <sys/param.h>
36#include <mach-o/ldsyms.h>
37#endif
38
39/**
40 * @brief Utility class to evaluate current usage of RAM
41 */
42class MemUsage
43{
44public:
45 /**
46 * @brief Get memory utilization in bytes
47 */
48 static inline
49 int64_t
50 Get ()
51 {
52#ifdef __linux__
53 struct proc_t usage;
54 look_up_our_self(&usage);
55 return usage.rss * getpagesize ();
56 // struct rusage usage;
57 //
58 // int ret = getrusage (RUSAGE_SELF, &usage);
59 // if (ret < 0)
60 // {
61 // os << "NA";
62 // return os;
63 // }
64 //
65 // os << (usage.ru_ixrss + usage.ru_idrss + usage.ru_isrss);
66 // return os;
67#endif
68#ifdef __APPLE__
69 task_t task = MACH_PORT_NULL;
70 struct task_basic_info t_info;
71 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
72
73 if (KERN_SUCCESS != task_info (mach_task_self (),
74 TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))
75 {
76 return -1; // something is wrong
77 }
78
79 return t_info.resident_size;
80#endif
81 // other systems are not yet supported
82 return -1;
83 }
84};
85
86#endif // MEM_USAGE_H