blob: d692d94538ce07063091a6c66a6af740db7ac6ce [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev41684ab2013-02-19 11:02:37 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev41684ab2013-02-19 11:02:37 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080019
20#ifndef MEM_USAGE_H
21#define MEM_USAGE_H
22
Saran Tarnoi9b569872013-03-28 14:26:11 -070023#ifdef __linux__
Alexander Afanasyevef91d292013-02-20 18:58:40 -080024// #include <proc/readproc.h>
25// #include <unistd.h>
26// // #include <sys/resource.h>
Saran Tarnoi9b569872013-03-28 14:26:11 -070027#include <sys/sysinfo.h>
28#endif
29
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080030#ifdef __APPLE__
31#include <mach/task.h>
32#include <mach/mach_traps.h>
33#include <mach/mach.h>
34#include <unistd.h>
35#include <err.h>
36#include <sys/param.h>
37#include <mach-o/ldsyms.h>
38#endif
39
40/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070041 * @ingroup ndn-helpers
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080042 * @brief Utility class to evaluate current usage of RAM
43 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044class MemUsage {
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080045public:
46 /**
47 * @brief Get memory utilization in bytes
48 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 static inline int64_t
50 Get()
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080051 {
Saran Tarnoi9b569872013-03-28 14:26:11 -070052#if defined(__linux__)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 /*
54 /proc/[pid]/statm
55 Provides information about memory usage, measured in pages. The
56 columns are:
Saran Tarnoi9b569872013-03-28 14:26:11 -070057
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 size (1) total program size
59 (same as VmSize in /proc/[pid]/status)
60 resident (2) resident set size
61 (same as VmRSS in /proc/[pid]/status)
62 share (3) shared pages (i.e., backed by a file)
63 text (4) text (code)
64 lib (5) library (unused in Linux 2.6)
65 data (6) data + stack
66 dt (7) dirty pages (unused in Linux 2.6)
Saran Tarnoi9b569872013-03-28 14:26:11 -070067
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 Reference: http://man7.org/linux/man-pages/man5/proc.5.html
69 */
70 std::ifstream is("/proc/self/statm");
71 if (!is.bad() && !is.eof()) {
72 unsigned long vm;
73 unsigned long rss;
74 is >> vm // the first number: virtual memory
75 >> rss; // the second number: resident set size
76
77 return rss * getpagesize();
78 }
79 else {
80 return -1;
81 }
Saran Tarnoi9b569872013-03-28 14:26:11 -070082
83#elif defined(__APPLE__)
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080084 struct task_basic_info t_info;
85 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
86
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 if (KERN_SUCCESS
88 != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) {
89 return -1; // something is wrong
90 }
Alexander Afanasyev41684ab2013-02-19 11:02:37 -080091
92 return t_info.resident_size;
93#endif
94 // other systems are not yet supported
95 return -1;
96 }
97};
98
99#endif // MEM_USAGE_H