FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairSystemInfo.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 /*
9  * FairSystemInfo.cxx
10  *
11  * Created on: Mar 18, 2015
12  * Author: f.uhlig
13  */
14 
15 #include "FairSystemInfo.h"
16 
17 #include <sys/resource.h>
18 #include <unistd.h>
19 
20 #if defined(__APPLE__) && defined(__MACH__)
21 #include <mach/mach.h>
22 
23 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
24 #include <limits>
25 #include <stdio.h>
26 
27 #else
28 #error "Unknown OS."
29 #endif
30 
32 {
33  // Returns the maximal used memory in MB
34 
35  struct rusage rusage;
36  getrusage(RUSAGE_SELF, &rusage);
37 
38 #if defined(__APPLE__) && defined(__MACH__)
39  return static_cast<Float_t>(rusage.ru_maxrss) / (1024 * 1024);
40 #else
41  return static_cast<Float_t>(rusage.ru_maxrss) / 1024;
42 #endif
43 }
44 
50 {
51 #if defined(__APPLE__) && defined(__MACH__)
52  /* OSX ------------------------------------------------------ */
53  struct mach_task_basic_info info;
54  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
55  if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), &infoCount)
56  != KERN_SUCCESS)
57  // (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
58  return static_cast<size_t>(0L); /* Can't access? */
59  return static_cast<size_t>(info.resident_size);
60 
61 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
62  /* Linux ---------------------------------------------------- */
63  long rss = 0L;
64  long pagesize = sysconf(_SC_PAGESIZE);
65  FILE* fp = NULL;
66  if ((fp = fopen("/proc/self/statm", "r")) == NULL)
67  return (size_t)0L; /* Can't open? */
68  if (fscanf(fp, "%*s%ld", &rss) != 1) {
69  fclose(fp);
70  return (size_t)0L; /* Can't read? */
71  }
72  fclose(fp);
73  if (rss > 0 && rss < std::numeric_limits<std::size_t>::max() / pagesize) {
74  return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
75  } else {
76  return (size_t)0L;
77  }
78 
79 #else
80  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
81  return (size_t)0L; /* Unsupported. */
82 #endif
83 }
Float_t GetMaxMemory()
size_t GetCurrentMemory()