Removing the --socket-mem eal parameter
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / progress.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <sys/time.h>
18 #include <iostream>
19 #include <cstdio>
20 #include <sstream>
21
22 #include "progress.hpp"
23
24 static uint64_t getSec()
25 {
26         struct timeval tv;
27
28         gettimeofday(&tv, NULL);
29         return tv.tv_sec;
30 }
31
32 Progress::Progress(size_t maxProgress, bool inPlace, bool showElapsedTime)
33         : maxProgress(maxProgress), curProgress(0), inPlace(inPlace), showElapsedTime(showElapsedTime), prevLength(0), title("Progress")
34 {
35         lastRefresh = -1;
36         firstRefresh = getSec();
37 }
38
39 void Progress::setProgress(size_t curProgress)
40 {
41         this->curProgress = curProgress;
42 }
43
44 void Progress::setProgress()
45 {
46         this->curProgress = maxProgress;
47 }
48
49 uint32_t Progress::addDetail(const string& detail)
50 {
51         details.push_back(make_pair(detail, 0));
52         return details.size() - 1;
53 }
54
55 void Progress::setDetail(uint32_t idx, uint32_t val)
56 {
57         details[idx].second = val;
58 }
59
60 bool Progress::couldRefresh()
61 {
62         uint32_t cur = getSec();
63
64         return (lastRefresh != cur);
65 }
66
67 void Progress::refresh(bool withNewLine)
68 {
69         lastRefresh = getSec();
70         uint64_t elapsed = lastRefresh - firstRefresh;
71         size_t progress = curProgress * 100 / maxProgress;
72         size_t remainingTime = curProgress? (elapsed * maxProgress - elapsed * curProgress) / curProgress : 0;
73
74         stringstream ss;
75
76         if (inPlace)
77                 ss << "\r";
78         ss << title << ": " << progress << "%";
79         ss << ", remaining: " << remainingTime;
80         if (showElapsedTime)
81                 ss << ", elapsed: " << elapsed;
82         for (size_t i = 0; i < details.size(); ++i)
83                 ss << ", " << details[i].first << ": " << details[i].second;
84
85         size_t prevLength2 = ss.str().size();
86
87         while (ss.str().size() < prevLength)
88                 ss << " ";
89         prevLength = prevLength2;
90
91         if (!inPlace || withNewLine)
92                 ss << "\n";
93
94         cout << ss.str();
95         cout.flush();
96 }