blob: 788a45ec6cdafe70e90534dd67ada6755a1d3dd1 [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
Saran Tarnoi9b569872013-03-28 14:26:11 -070024#ifdef __linux__
Alexander Afanasyevef91d292013-02-20 18:58:40 -080025// #include <proc/readproc.h>
26// #include <unistd.h>
27// // #include <sys/resource.h>
Saran Tarnoi9b569872013-03-28 14:26:11 -070028#include <sys/sysinfo.h>
29#endif
30
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080031#ifdef __APPLE__
32#include <mach/task.h>
33#include <mach/mach_traps.h>
34#include <mach/mach.h>
35#include <unistd.h>
36#include <err.h>
37#include <sys/param.h>
38#include <mach-o/ldsyms.h>
39#endif
40
41/**
42 * @brief Utility class to evaluate current usage of RAM
43 */
44class MemUsage
45{
46public:
47 /**
48 * @brief Get memory utilization in bytes
49 */
50 static inline
51 int64_t
52 Get ()
53 {
Saran Tarnoi9b569872013-03-28 14:26:11 -070054#if defined(__linux__)
55/*
56/proc/[pid]/statm
57 Provides information about memory usage, measured in pages. The
58 columns are:
59
60 size (1) total program size
61 (same as VmSize in /proc/[pid]/status)
62 resident (2) resident set size
63 (same as VmRSS in /proc/[pid]/status)
64 share (3) shared pages (i.e., backed by a file)
65 text (4) text (code)
66 lib (5) library (unused in Linux 2.6)
67 data (6) data + stack
68 dt (7) dirty pages (unused in Linux 2.6)
69
70Reference: http://man7.org/linux/man-pages/man5/proc.5.html
71*/
72 std::ifstream is ("/proc/self/statm");
73 if (!is.bad () && !is.eof ())
74 {
75 unsigned long vm;
76 unsigned long rss;
77 is >> vm // the first number: virtual memory
78 >> rss; // the second number: resident set size
79
80 return rss * getpagesize ();
81 }
82 else
83 {
84 return -1;
85 }
86
87#elif defined(__APPLE__)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080088 struct task_basic_info t_info;
89 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
90
91 if (KERN_SUCCESS != task_info (mach_task_self (),
92 TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))
93 {
94 return -1; // something is wrong
95 }
96
97 return t_info.resident_size;
98#endif
99 // other systems are not yet supported
100 return -1;
101 }
102};
103
104#endif // MEM_USAGE_H