OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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 "Structure/SDDSWriter.h"
25 
26 #include <fstream>
27 #include <map>
28 #include <string>
29 #include <vector>
30 
31 
32 class MemoryProfiler: public SDDSWriter {
33  /* Pay attention with units. /proc/[pid]/status returns values in
34  * KiB (Kibibyte) although the units say kB.
35  * KiB has base 2 not base 10
36  */
37 
38 public:
39  typedef std::vector<long double> vm_t;
40  typedef std::vector<std::string> units_t;
41 
42  MemoryProfiler(const std::string& fname, bool restart);
43 
45  VMPEAK = 0, // VmPeak: Peak virtual memory size.
46  VMSIZE, // VmSize: Virtual memory size.
47  VMHWM, // VmHWM: Peak resident set size ("high water mark").
48  VMRSS, // VmRSS: Resident set size.
49  VMDATA, // VmData: Size of data.
50  VMSTK, // VmStk: Size of stack.
51  VMEXE, // VmExe: Size of text segments.
52  VMLCK, // VmLck: Locked memory size (see mlock(3)).
53  VMPIN, // VmPin: Pinned memory size (since Linux 3.2). These are pages that can't be moved because something
54  // needs to directly access physical memory.
55  VMLIB, // VmLib: Shared library code size.
56  VMPTE, // VmPTE: Page table entries size (since Linux 2.6.10).
57  VMPMD, // VmPMD: Size of second-level page tables (since Linux 4.0).
58  VMSWAP // VmSwap: Swapped-out virtual memory size by anonymous private pages; shmem swap usage is not
59  // included (since Linux 2.6.34).
60  };
61 
62  void write(PartBunchBase<double, 3>* beam) override;
63 
64 private:
65  void header();
66  void update();
67  void compute(vm_t& vmMin, vm_t& vmMax, vm_t& vmAvg);
68 
69 private:
70  std::map<std::string, int> procinfo_m;
73 };
74 
75 #endif
MemoryProfiler(const std::string &fname, bool restart)
void write(PartBunchBase< double, 3 > *beam) override
std::vector< long double > vm_t
void compute(vm_t &vmMin, vm_t &vmMax, vm_t &vmAvg)
std::map< std::string, int > procinfo_m
std::vector< std::string > units_t