Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
Alexander Afanasyev | ef91d29 | 2013-02-20 18:58:40 -0800 | [diff] [blame] | 24 | // #ifdef __linux__ |
| 25 | // #include <proc/readproc.h> |
| 26 | // #include <unistd.h> |
| 27 | // // #include <sys/resource.h> |
| 28 | // #endif |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 29 | #ifdef __APPLE__ |
| 30 | #include <mach/task.h> |
| 31 | #include <mach/mach_traps.h> |
| 32 | #include <mach/mach.h> |
| 33 | #include <unistd.h> |
| 34 | #include <err.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <mach-o/ldsyms.h> |
| 37 | #endif |
| 38 | |
| 39 | /** |
| 40 | * @brief Utility class to evaluate current usage of RAM |
| 41 | */ |
| 42 | class MemUsage |
| 43 | { |
| 44 | public: |
| 45 | /** |
| 46 | * @brief Get memory utilization in bytes |
| 47 | */ |
| 48 | static inline |
| 49 | int64_t |
| 50 | Get () |
| 51 | { |
Alexander Afanasyev | ef91d29 | 2013-02-20 18:58:40 -0800 | [diff] [blame] | 52 | // #ifdef __linux__ |
| 53 | // struct proc_t usage; |
| 54 | // look_up_our_self(&usage); |
| 55 | // return usage.rss * getpagesize (); |
| 56 | // // struct rusage usage; |
| 57 | // // |
| 58 | // // int ret = getrusage (RUSAGE_SELF, &usage); |
| 59 | // // if (ret < 0) |
| 60 | // // { |
| 61 | // // os << "NA"; |
| 62 | // // return os; |
| 63 | // // } |
| 64 | // // |
| 65 | // // os << (usage.ru_ixrss + usage.ru_idrss + usage.ru_isrss); |
| 66 | // // return os; |
| 67 | // #endif |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 68 | #ifdef __APPLE__ |
Alexander Afanasyev | 41684ab | 2013-02-19 11:02:37 -0800 | [diff] [blame] | 69 | struct task_basic_info t_info; |
| 70 | mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; |
| 71 | |
| 72 | if (KERN_SUCCESS != task_info (mach_task_self (), |
| 73 | TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) |
| 74 | { |
| 75 | return -1; // something is wrong |
| 76 | } |
| 77 | |
| 78 | return t_info.resident_size; |
| 79 | #endif |
| 80 | // other systems are not yet supported |
| 81 | return -1; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | #endif // MEM_USAGE_H |