source: src/Common/Stats/Time.cc @ 79eaeb7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 79eaeb7 was 79eaeb7, checked in by tdelisle <tdelisle@…>, 5 years ago

Improved printing, parent printing still incorrect

  • Property mode set to 100644
File size: 4.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Time.cc --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Mar 04 15:16:07 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#include "Time.h"
17
18#include <cassert>
19#include <chrono>
20#include <cstdint>
21#include <cstring>
22#include <iostream>
23#include <iomanip>
24#include <stack>
25
26namespace Stats {
27        namespace Time {
28#               if !defined(NO_TIME_STATISTICS)
29                        extern bool enabled;
30
31                        Base::TreeTop top;
32
33                        typedef  std::chrono::time_point<std::chrono::high_resolution_clock> point_t;
34                        std::chrono::duration<double> total;
35                        std::chrono::duration<double> parent;
36
37                        point_t global_begin;
38
39                        template<typename T>
40                        static inline std::ostream & operator<<(std::ostream & os, const std::chrono::duration<T> & dd) {
41                                auto d = std::chrono::duration_cast<std::chrono::milliseconds>(dd);
42                                auto minutes = std::chrono::duration_cast<std::chrono::minutes>(d);
43                                auto seconds = std::chrono::duration_cast<std::chrono::seconds>(d % std::chrono::minutes(1));
44                                auto millis  = std::chrono::duration_cast<std::chrono::milliseconds>(d % std::chrono::seconds(1));
45
46                                bool zmin = minutes == minutes.zero();
47                                bool zsec = seconds == seconds.zero();
48                                bool zmil = millis  == millis .zero();
49
50                                if(!zmin) {
51                                        os << std::setw(4) << minutes.count() << "m";
52                                } else {
53                                        os << std::string(5, ' ');
54                                }
55
56                                if(!zmin || !zsec) {
57                                        if(!zmin) os << std::setfill('0');
58                                        os << std::setw(2) << seconds.count() << "s";
59                                } else {
60                                        os << std::string(3, ' ');
61                                }
62                                os << std::setfill(' ');
63
64                                if(!zmin || !zsec || !zmil) {
65                                        if(!zmin || !zsec) os << std::setfill('0');
66                                        os << std::setw(3) << millis .count();
67                                } else {
68                                        os << std::string(4, ' ');
69                                }
70                                os << std::setfill(' ');
71
72                                return os;
73                        }
74
75                        class TimerNode : public Base::Tree<top> {
76                        public:
77                                TimerNode(const char * const name )
78                                        : Base::Tree<top>(name)
79                                {}
80
81                                TimerNode(const char * const name, Base::Tree<top> * parent)
82                                        : Base::Tree<top>(name, parent)
83
84                                {}
85
86                                virtual void print(std::ostream & os) override {
87                                        assert(finished);
88                                        std::chrono::duration<double> diff = end - begin;
89                                        os << diff << " | ";
90                                        os << std::setw(7) << std::setprecision(0);
91                                        os << size_t(100.0 * diff.count() / total.count()) << "% | ";
92                                        os << std::setw(5) << std::setprecision(0);
93                                        os << size_t(100.0 * diff.count() / total.count()) << "% ";
94                                }
95
96                                void start() {
97                                        begin = std::chrono::high_resolution_clock::now();
98                                }
99
100                                void finish() {
101                                        end = std::chrono::high_resolution_clock::now();
102                                        finished = true;
103                                }
104
105                        protected:
106                                virtual ~TimerNode() = default;
107
108                        private:
109                                bool finished = false;
110
111                                point_t begin;
112                                point_t end;
113                        };
114
115                        std::stack<TimerNode *> nodes;
116
117                        void StartGlobal() {
118                                global_begin = std::chrono::high_resolution_clock::now();
119                        }
120
121                        void StartBlock(const char * const name) {
122                                if(!enabled) return;
123                                auto node = nodes.empty()
124                                        ? new TimerNode(name)
125                                        : new TimerNode(name, nodes.top());
126
127                                nodes.push(node);
128                                node->start();
129                        }
130
131                        void StopBlock() {
132                                if(!enabled) return;
133                                nodes.top()->finish();
134                                nodes.pop();
135                        }
136
137                        void print() {
138                                if(!top.head) return;
139                                auto global_end = std::chrono::high_resolution_clock::now();
140                                total = global_end - global_begin;
141                                parent = total;
142
143                                size_t nc = 0;
144                                Base::ForAll(top, 0, [&](Base::TreeImpl * node, size_t level) {
145                                        nc = std::max(nc, (4 * level) + std::strlen(node->name));
146                                });
147
148                                size_t nct = nc + 37;
149                                std::cerr << std::string(nct, '=') << std::endl;
150                                const char * const title = "Timing Results";
151                                std::cerr << std::string((nct - std::strlen(title)) / 2, ' ');
152                                std::cerr << title << std::endl;
153                                std::cerr << std::string(nct, '-') << std::endl;
154                                std::cerr << "Location";
155                                std::cerr << std::string(nc - (std::strlen("Location")), ' ');
156                                std::cerr << " | ";
157                                std::cerr << "       Time | ";
158                                std::cerr << "% parent | ";
159                                std::cerr << "% total |" << std::endl;
160                                std::cerr << std::string(nct, '-') << std::endl;
161
162                                Base::ForAll(top, 0, [&](Base::TreeImpl * node, size_t level) {
163                                        std::cerr << std::string(level * 4, ' ');
164                                        std::cerr << node->name;
165                                        std::cerr << std::string(nc - ((level * 4) + std::strlen(node->name)), ' ');
166                                        std::cerr << " | ";
167                                        node->print(std::cerr);
168                                        std::cerr << " |";
169                                        std::cerr << '\n';
170                                }, true);
171
172                                std::cerr << std::string(nct, '-') << std::endl;
173                                std::cerr << "Total " << total << std::endl;
174                                std::cerr << std::string(nct, '-') << std::endl;
175                        }
176#               endif
177        }
178}
Note: See TracBrowser for help on using the repository browser.