Project
Loading...
Searching...
No Matches
FairRunInfo.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#include "FairRunInfo.h"
9
10#include "FairLogger.h" // for FairLogger
11
12#include <TFile.h> // for TFile, gFile
13#include <TH1.h> // for TH1F
14#include <TIterator.h> // for TIterator
15#include <TList.h> // for TList
16#include <TString.h> // for TString
17#include <TSystem.h> // for ProcInfo_t, TSystem, etc
18#include <algorithm> // for sort
19
21
23 : TObject()
24 , fTimeStamp()
25 , fCpuInfo()
26 , fMemInfo()
27 , fProcInfo()
28 , fTimeDiff()
29 , fTime()
30 , fResidentMemory()
31 , fVirtualMemory()
32{}
33
35
37{
38 // Extract the Information about the used memory from the system.
39 // Stores this info together with the timedifference between now
40 // and the last call in container arrays for later usage.
41 // gSystem->GetCpuInfo(&cpuInfo, 10);
42 // gSystem->GetMemInfo(&memInfo);
43
44 GetInfo();
45
46 CalculateTimeDifference();
47
48 PrintInfo();
49}
50
51void FairRunInfo::GetInfo()
52{
53 // Set the TimeStamp to the actual time and store it
54 fTimeStamp.Set();
55 // fTime.push_back(fTimeStamp.GetSec());
56 fTime.push_back(fTimeStamp.AsDouble());
57
58 gSystem->GetProcInfo(&fProcInfo);
59 fResidentMemory.push_back(fProcInfo.fMemResident / 1024);
60 fVirtualMemory.push_back(fProcInfo.fMemVirtual / 1024);
61}
62
63void FairRunInfo::CalculateTimeDifference()
64{
65 // Calculates the time difference between now and the last call
66
67 Int_t lastElement = fTime.size() - 1;
68 fTimeDiff.push_back(fTime.at(lastElement) - fTime.at(lastElement - 1));
69}
70
71void FairRunInfo::PrintInfo()
72{
73
74 LOG(debug) << "Time to execute 1 event: " << fTimeDiff.back() << "s";
75 LOG(debug) << "Used resident memory: " << fResidentMemory.back() << " MB";
76 LOG(debug) << "Used virtual memory: " << fVirtualMemory.back() << " MB";
77}
78
80{
81 TList* histoList = new TList();
82 CreateAndFillHistograms(histoList);
83 // CreateAndFillHistograms(histoList);
84 WriteHistosToFile(histoList);
85}
86
87void FairRunInfo::CreateAndFillHistograms(TList* histoList)
88{
89 Int_t entries = fTime.size();
90 Double_t timePeriod = fTime.back() - fTime.front();
91 Int_t timeBins = static_cast<Int_t>(timePeriod * 10);
92 if (0 == timeBins)
93 timeBins = 1;
94
95 TH1F* ResidentMemoryVsEvent = new TH1F(
96 "ResidentMemoryVsEvent", "Resident Memory as function of Eventnumber;Event;Memory [MB]", entries, 0, entries);
97 TH1F* VirtualMemoryVsEvent = new TH1F(
98 "VirtualMemoryVsEvent", "Virtual Memory as function of Eventnumber;Event;Memory [MB]", entries, 0, entries);
99 TH1F* ResidentMemoryVsTime = new TH1F(
100 "ResidentMemoryVsTime", "Resident memory as function of Runtime;Time [s];Memory [MB]", timeBins, 0, timeBins);
101 TH1F* VirtualMemoryVsTime = new TH1F(
102 "VirtualMemoryVsTime", "Virtual memory as function of Runtime;Time [s];Memory [MB]", timeBins, 0, timeBins);
103 TH1F* EventtimeVsEvent = new TH1F(
104 "EventtimeVsEvent", "Runtime per Event as function of Event number;Event;Time [s]", entries - 1, 1, entries);
105
106 std::vector<Double_t> timeDiffSorted(fTimeDiff);
107 std::vector<Double_t>::iterator it;
108 std::sort(timeDiffSorted.begin(), timeDiffSorted.end());
109
110 Double_t minTime = timeDiffSorted.front();
111 Double_t maxTime = timeDiffSorted.back();
112 timePeriod = maxTime - minTime;
113
114 TH1F* TimePerEvent = new TH1F("TimePerEvent",
115 "Runtime;Time [s];Events",
116 static_cast<Int_t>((timePeriod + 20) * 10),
117 static_cast<Int_t>(minTime - 10),
118 static_cast<Int_t>(maxTime + 10));
119
120 Int_t counter = 0;
121 std::vector<Long_t>::iterator lit;
122 Double_t timeOffset = fTime.front() + 1.;
123 for (lit = fResidentMemory.begin(); lit < fResidentMemory.end(); ++lit) {
124 ResidentMemoryVsEvent->Fill(counter, *lit);
125 ResidentMemoryVsTime->Fill(fTime.at(counter) - timeOffset, *lit);
126 counter++;
127 }
128 histoList->AddLast(ResidentMemoryVsEvent);
129 histoList->AddLast(ResidentMemoryVsTime);
130
131 counter = 0;
132 for (lit = fVirtualMemory.begin(); lit < fVirtualMemory.end(); ++lit) {
133 VirtualMemoryVsEvent->Fill(counter, *lit);
134 VirtualMemoryVsTime->Fill(fTime.at(counter) - timeOffset, *lit);
135 counter++;
136 }
137 histoList->AddLast(VirtualMemoryVsEvent);
138 histoList->AddLast(VirtualMemoryVsTime);
139
140 counter = 1;
141 for (it = fTimeDiff.begin(); it < fTimeDiff.end(); ++it) {
142 TimePerEvent->Fill(*it);
143 EventtimeVsEvent->Fill(counter, *it);
144 counter++;
145 }
146 histoList->AddLast(TimePerEvent);
147 histoList->AddLast(EventtimeVsEvent);
148}
149
150void FairRunInfo::WriteHistosToFile(TList* histoList)
151{
152 // If the file size is larger then approx. 2GB then the histos
153 // can't be read any longer. Because of this problem the histos
154 // are written to a separate file instead
155
156 TFile* oldfile = gFile;
157
158 TString directory = gFile->GetName();
159 LOG(debug) << "Name: " << gFile->GetName();
160 Ssiz_t posLastSlash = directory.Last('/');
161 directory.Remove(posLastSlash + 1, directory.Length() - posLastSlash - 1);
162 TString filename = "";
163 if (directory.EndsWith("/")) {
164 filename += directory;
165 }
166
167 directory = gFile->GetName();
168 LOG(debug) << "Name: " << directory.Data();
169 posLastSlash = directory.Last('/');
170 directory.Remove(0, posLastSlash + 1);
171 directory.ReplaceAll(".root", "");
172 LOG(debug) << "Name: " << directory.Data();
173
174 // Int_t pid = gSystem->GetPid();
175 filename += "FairRunInfo_";
176 filename += directory;
177 filename += ".root";
178 LOG(debug) << "Filename: " << filename.Data();
179
180 TFile* f1 = TFile::Open(filename, "recreate");
181 f1->cd();
182
183 TIterator* listIter = histoList->MakeIterator();
184 listIter->Reset();
185 TObject* obj = nullptr;
186 while ((obj = listIter->Next())) {
187 obj->Write();
188 }
189
190 delete listIter;
191 histoList->Delete();
192 delete histoList;
193
194 f1->Close();
195 f1->Delete();
196 gFile = oldfile;
197}
198
199void FairRunInfo::Reset() { GetInfo(); }
std::ostringstream debug
ClassImp(FairRunInfo)
std::vector< long > entries
void StoreInfo()
void WriteInfo()
value_T f1
Definition TrackUtils.h:91
std::string filename()
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"