Line data Source code
1 : /* Memory usage utilities. 2 : 3 : Copied from CADO-NFS. 4 : 5 : This file is part of the ECM Library. 6 : 7 : The ECM Library is free software; you can redistribute it and/or modify 8 : it under the terms of the GNU Lesser General Public License as published by 9 : the Free Software Foundation; either version 3 of the License, or (at your 10 : option) any later version. 11 : 12 : The ECM Library is distributed in the hope that it will be useful, but 13 : WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 : or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 : License for more details. 16 : 17 : You should have received a copy of the GNU Lesser General Public License 18 : along with the ECM Library; see the file COPYING.LIB. If not, see 19 : http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 20 : 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 21 : 22 : #ifndef _WIN32 23 : 24 : #include <stdio.h> 25 : #include <sys/types.h> 26 : #include <unistd.h> 27 : #include "ecm-impl.h" 28 : 29 : /* Returns peak memory usage, in KB 30 : * This is the VmPeak field in the status file of /proc/pid/ dir 31 : * This is highly non portable. 32 : * Return -1 in case of failure. 33 : */ 34 : long 35 142 : PeakMemusage (void) 36 : { 37 142 : pid_t pid = getpid (); 38 : 39 : char str[1024]; 40 : char *truc; 41 142 : snprintf (str, 1024, "/proc/%d/status", (int) pid); 42 : 43 : FILE *file; 44 142 : file = fopen (str, "r"); 45 142 : if (file == NULL) 46 0 : return -1; /* for example on Mac OS X */ 47 : 48 : long mem; 49 : for(;;) 50 2272 : { 51 2414 : truc = fgets (str, 1023, file); 52 2414 : if (truc == NULL) 53 0 : return -1; /* for example on FreeBSD */ 54 2414 : int ret = sscanf (str, "VmPeak: %ld", &mem); 55 2414 : if (ret == 1) 56 : { 57 142 : fclose (file); 58 142 : return mem; 59 : } 60 : } 61 : } 62 : 63 : #else 64 : 65 : #include <windows.h> 66 : #include <psapi.h> 67 : 68 : long 69 : PeakMemusage (void) 70 : { 71 : PROCESS_MEMORY_COUNTERS info; 72 : GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); 73 : return (long)(info.PeakWorkingSetSize >> 10); 74 : } 75 : 76 : #endif