OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
MemoryProfiler.h
Go to the documentation of this file.
1 //
2 // Class MemoryProfiler
3 // This class writes a SDDS file with virtual memory usage information (Linux only).
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // Implemented as part of the PhD thesis
9 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #ifndef OPAL_MEMORY_PROFILER_H
22 #define OPAL_MEMORY_PROFILER_H
23 
24 #include <fstream>
25 #include <string>
26 #include <map>
27 #include <vector>
28 
29 #include "SDDSWriter.h"
30 
31 class MemoryProfiler : public SDDSWriter {
32  /* Pay attention with units. /proc/[pid]/status returns values in
33  * KiB (Kibibyte) although the units say kB.
34  * KiB has base 2 not base 10
35  */
36 
37 public:
38  typedef std::vector<long double> vm_t;
39  typedef std::vector<std::string> units_t;
40 
41  MemoryProfiler(const std::string& fname, bool restart);
42 
44  VMPEAK = 0, // VmPeak: Peak virtual memory size.
45  VMSIZE, // VmSize: Virtual memory size.
46  VMHWM, // VmHWM: Peak resident set size ("high water mark").
47  VMRSS, // VmRSS: Resident set size.
48  VMDATA, // VmData: Size of data.
49  VMSTK, // VmStk: Size of stack.
50  VMEXE, // VmExe: Size of text segments.
51  VMLCK, // VmLck: Locked memory size (see mlock(3)).
52  VMPIN, // VmPin: Pinned memory size (since Linux 3.2). These are pages that can't be moved because something
53  // needs to directly access physical memory.
54  VMLIB, // VmLib: Shared library code size.
55  VMPTE, // VmPTE: Page table entries size (since Linux 2.6.10).
56  VMPMD, // VmPMD: Size of second-level page tables (since Linux 4.0).
57  VMSWAP // VmSwap: Swapped-out virtual memory size by anonymous private pages; shmem swap usage is not
58  // included (since Linux 2.6.34).
59  };
60 
61  void write(const PartBunchBase<double, 3> *beam) override;
62 
63 private:
64  void header();
65  void update();
66  void compute(vm_t& vmMin, vm_t& vmMax, vm_t& vmAvg);
67 
68 private:
69  std::map<std::string, int> procinfo_m;
72 };
73 
74 #endif
void write(const PartBunchBase< double, 3 > *beam) override
std::vector< std::string > units_t
void compute(vm_t &vmMin, vm_t &vmMax, vm_t &vmAvg)
std::vector< long double > vm_t
MemoryProfiler(const std::string &fname, bool restart)
std::map< std::string, int > procinfo_m