blob: a18448a05a24c573e6be67520bfacaeb0ec267a4 [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/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070042 * @ingroup ndn-helpers
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080043 * @brief Utility class to evaluate current usage of RAM
44 */
45class MemUsage
46{
47public:
48 /**
49 * @brief Get memory utilization in bytes
50 */
51 static inline
52 int64_t
53 Get ()
54 {
Saran Tarnoi9b569872013-03-28 14:26:11 -070055#if defined(__linux__)
56/*
57/proc/[pid]/statm
58 Provides information about memory usage, measured in pages. The
59 columns are:
60
61 size (1) total program size
62 (same as VmSize in /proc/[pid]/status)
63 resident (2) resident set size
64 (same as VmRSS in /proc/[pid]/status)
65 share (3) shared pages (i.e., backed by a file)
66 text (4) text (code)
67 lib (5) library (unused in Linux 2.6)
68 data (6) data + stack
69 dt (7) dirty pages (unused in Linux 2.6)
70
71Reference: http://man7.org/linux/man-pages/man5/proc.5.html
72*/
73 std::ifstream is ("/proc/self/statm");
74 if (!is.bad () && !is.eof ())
75 {
76 unsigned long vm;
77 unsigned long rss;
78 is >> vm // the first number: virtual memory
79 >> rss; // the second number: resident set size
80
81 return rss * getpagesize ();
82 }
83 else
84 {
85 return -1;
86 }
87
88#elif defined(__APPLE__)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080089 struct task_basic_info t_info;
90 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
91
92 if (KERN_SUCCESS != task_info (mach_task_self (),
93 TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))
94 {
95 return -1; // something is wrong
96 }
97
98 return t_info.resident_size;
99#endif
100 // other systems are not yet supported
101 return -1;
102 }
103};
104
105#endif // MEM_USAGE_H