Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 19 | |
| 20 | #ifndef MEM_USAGE_H |
| 21 | #define MEM_USAGE_H |
| 22 | |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 23 | #ifdef __linux__ |
Alexander Afanasyev | ef91d29 | 2013-02-20 18:58:40 -0800 | [diff] [blame] | 24 | // #include <proc/readproc.h> |
| 25 | // #include <unistd.h> |
| 26 | // // #include <sys/resource.h> |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 27 | #include <sys/sysinfo.h> |
| 28 | #endif |
| 29 | |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 30 | #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 Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 41 | * @ingroup ndn-helpers |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 42 | * @brief Utility class to evaluate current usage of RAM |
| 43 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | class MemUsage { |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 45 | public: |
| 46 | /** |
| 47 | * @brief Get memory utilization in bytes |
| 48 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 49 | static inline int64_t |
| 50 | Get() |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 51 | { |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 52 | #if defined(__linux__) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | /* |
| 54 | /proc/[pid]/statm |
| 55 | Provides information about memory usage, measured in pages. The |
| 56 | columns are: |
Saran Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 58 | 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 Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | 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 Tarnoi | 9b56987 | 2013-03-28 14:26:11 -0700 | [diff] [blame] | 82 | |
| 83 | #elif defined(__APPLE__) |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 84 | struct task_basic_info t_info; |
| 85 | mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; |
| 86 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | 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 Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 91 | |
| 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 |