| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2013-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <fstream> |
| 21 | |
| 22 | #include "MemoryStat.h" |
| 23 | #include "globaldef.h" |
| 24 | |
| 25 | // set to 1 for memory logging |
| 26 | #ifndef USE_MEMORY_LOG |
| 27 | # define USE_MEMORY_LOG 0 |
| 28 | #endif |
| 29 | |
| 30 | std::ofstream StatLogFile |
| 31 | #if USE_MEMORY_LOG |
| 32 | ("MemoryLog.txt") |
| 33 | #endif |
| 34 | ; |
| 35 | |
| 36 | #if defined(__APPLE__) |
| 37 | # include <mach/mach.h> |
| 38 | #elif defined(_WINDOWS) |
| 39 | # include <windows.h> |
| 40 | # include <psapi.h> |
| 41 | #endif |
| 42 | |
| 43 | static size_t getProcessMemory ( bool resident ATTR_UNUSED = true ) |
| 44 | { |
| 45 | #ifdef __APPLE__ |
| 46 | struct task_basic_info t_info; |
| 47 | mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; |
| 48 | |
| 49 | if (KERN_SUCCESS != task_info(mach_task_self(), |
| 50 | TASK_BASIC_INFO, (task_info_t)&t_info, |
| 51 | &t_info_count)) |
| 52 | return 0; |
| 53 | return resident ? t_info.resident_size : t_info.virtual_size; |
| 54 | #elif defined(_WINDOWS) |
| 55 | PROCESS_MEMORY_COUNTERS pmc; |
| 56 | if ( 0 == GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) ) |
| 57 | return 0; |
| 58 | // return resident ? pmc.WorkingSetSize : pmc.PrivateUsage; |
| 59 | return resident ? pmc.WorkingSetSize : pmc.PagefileUsage; |
| 60 | #else // undefined platform |
| 61 | return 0; |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | MemoryStatistics :: MemoryStatistics ( const std::string& name ) |
| 66 | : operation(name) |
| 67 | { |
| 68 | if ( USE_MEMORY_LOG ) |
| 69 | { |
| 70 | timer.Start(); |
| 71 | startMem = getProcessMemory()/1024/1024; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | MemoryStatistics :: ~MemoryStatistics() |
| 76 | { |
| 77 | if ( USE_MEMORY_LOG ) |
| 78 | { |
| 79 | timer.Stop(); |
| 80 | size_t endMem = getProcessMemory()/1024/1024; |
| 81 | StatLogFile << operation << ": time " << timer << " sec, op memory " << endMem-startMem << " Mb, total memory " << endMem << " Mb\n"; |
| 82 | timer.Reset(); |
| 83 | } |
| 84 | } |