blob: e58b9adb1447d3b57b95f8614c89b2e8aeffd4cc [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 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045class MemUsage {
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080046public:
47 /**
48 * @brief Get memory utilization in bytes
49 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 static inline int64_t
51 Get()
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080052 {
Saran Tarnoi9b569872013-03-28 14:26:11 -070053#if defined(__linux__)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 /*
55 /proc/[pid]/statm
56 Provides information about memory usage, measured in pages. The
57 columns are:
Saran Tarnoi9b569872013-03-28 14:26:11 -070058
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 size (1) total program size
60 (same as VmSize in /proc/[pid]/status)
61 resident (2) resident set size
62 (same as VmRSS in /proc/[pid]/status)
63 share (3) shared pages (i.e., backed by a file)
64 text (4) text (code)
65 lib (5) library (unused in Linux 2.6)
66 data (6) data + stack
67 dt (7) dirty pages (unused in Linux 2.6)
Saran Tarnoi9b569872013-03-28 14:26:11 -070068
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 Reference: http://man7.org/linux/man-pages/man5/proc.5.html
70 */
71 std::ifstream is("/proc/self/statm");
72 if (!is.bad() && !is.eof()) {
73 unsigned long vm;
74 unsigned long rss;
75 is >> vm // the first number: virtual memory
76 >> rss; // the second number: resident set size
77
78 return rss * getpagesize();
79 }
80 else {
81 return -1;
82 }
Saran Tarnoi9b569872013-03-28 14:26:11 -070083
84#elif defined(__APPLE__)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080085 struct task_basic_info t_info;
86 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
87
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 if (KERN_SUCCESS
89 != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) {
90 return -1; // something is wrong
91 }
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080092
93 return t_info.resident_size;
94#endif
95 // other systems are not yet supported
96 return -1;
97 }
98};
99
100#endif // MEM_USAGE_H