| 1 | /*
 | 
| 2 |  * Souffle - A Datalog Compiler
 | 
| 3 |  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
 | 
| 4 |  * Licensed under the Universal Permissive License v 1.0 as shown at:
 | 
| 5 |  * - https://opensource.org/licenses/UPL
 | 
| 6 |  * - <souffle root>/licenses/SOUFFLE-UPL.txt
 | 
| 7 |  */
 | 
| 8 | 
 | 
| 9 | /************************************************************************
 | 
| 10 |  *
 | 
| 11 |  * @file Logger.h
 | 
| 12 |  *
 | 
| 13 |  * A logger is the utility utilized by RAM programs to create logs and
 | 
| 14 |  * traces.
 | 
| 15 |  *
 | 
| 16 |  ***********************************************************************/
 | 
| 17 | 
 | 
| 18 | #pragma once
 | 
| 19 | 
 | 
| 20 | #include "souffle/profile/ProfileEvent.h"
 | 
| 21 | #include "souffle/utility/MiscUtil.h"
 | 
| 22 | #include <cstddef>
 | 
| 23 | #include <functional>
 | 
| 24 | #include <string>
 | 
| 25 | #include <utility>
 | 
| 26 | 
 | 
| 27 | namespace souffle {
 | 
| 28 | 
 | 
| 29 | /**
 | 
| 30 |  * The class utilized to times for the souffle profiling tool. This class
 | 
| 31 |  * is utilized by both -- the interpreted and compiled version -- to conduct
 | 
| 32 |  * the corresponding measurements.
 | 
| 33 |  *
 | 
| 34 |  * To far, only execution times are logged. More events, e.g. the number of
 | 
| 35 |  * processed tuples may be added in the future.
 | 
| 36 |  */
 | 
| 37 | class Logger {
 | 
| 38 | public:
 | 
| 39 |     Logger(std::string label, std::size_t iteration) : Logger(label, iteration, []() { return 0; }) {}
 | 
| 40 | 
 | 
| 41 |     Logger(std::string label, std::size_t iteration, std::function<std::size_t()> size)
 | 
| 42 |             : label(std::move(label)), start(now()), iteration(iteration), size(size), preSize(size()) {
 | 
| 43 | #ifdef WIN32
 | 
| 44 |         HANDLE hProcess = GetCurrentProcess();
 | 
| 45 |         PROCESS_MEMORY_COUNTERS processMemoryCounters;
 | 
| 46 |         GetProcessMemoryInfo(hProcess, &processMemoryCounters, sizeof(processMemoryCounters));
 | 
| 47 |         startMaxRSS = processMemoryCounters.PeakWorkingSetSize / 1000;
 | 
| 48 | #else
 | 
| 49 |         struct rusage ru {};
 | 
| 50 |         getrusage(RUSAGE_SELF, &ru);
 | 
| 51 |         startMaxRSS = ru.ru_maxrss;
 | 
| 52 | #endif  // WIN32
 | 
| 53 |         // Assume that if we are logging the progress of an event then we care about usage during that time.
 | 
| 54 |         ProfileEventSingleton::instance().resetTimerInterval();
 | 
| 55 |     }
 | 
| 56 | 
 | 
| 57 |     ~Logger() {
 | 
| 58 | #ifdef WIN32
 | 
| 59 |         HANDLE hProcess = GetCurrentProcess();
 | 
| 60 |         PROCESS_MEMORY_COUNTERS processMemoryCounters;
 | 
| 61 |         GetProcessMemoryInfo(hProcess, &processMemoryCounters, sizeof(processMemoryCounters));
 | 
| 62 |         std::size_t endMaxRSS = processMemoryCounters.PeakWorkingSetSize / 1000;
 | 
| 63 | #else
 | 
| 64 |         struct rusage ru {};
 | 
| 65 |         getrusage(RUSAGE_SELF, &ru);
 | 
| 66 |         std::size_t endMaxRSS = ru.ru_maxrss;
 | 
| 67 | #endif  // WIN32
 | 
| 68 |         ProfileEventSingleton::instance().makeTimingEvent(
 | 
| 69 |                 label, start, now(), startMaxRSS, endMaxRSS, size() - preSize, iteration);
 | 
| 70 |     }
 | 
| 71 | 
 | 
| 72 | private:
 | 
| 73 |     std::string label;
 | 
| 74 |     time_point start;
 | 
| 75 |     std::size_t startMaxRSS;
 | 
| 76 |     std::size_t iteration;
 | 
| 77 |     std::function<std::size_t()> size;
 | 
| 78 |     std::size_t preSize;
 | 
| 79 | };
 | 
| 80 | }  // end of namespace souffle
 |