Ninja
status_printer.h
Go to the documentation of this file.
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <cstdint>
17 #include <queue>
18 
19 #include "exit_status.h"
20 #include "explanations.h"
21 #include "line_printer.h"
22 #include "status.h"
23 
24 /// Implementation of the Status interface that prints the status as
25 /// human-readable strings to stdout
27  explicit StatusPrinter(const BuildConfig& config);
28 
29  /// Callbacks for the Plan to notify us about adding/removing Edge's.
30  void EdgeAddedToPlan(const Edge* edge) override;
31  void EdgeRemovedFromPlan(const Edge* edge) override;
32 
33  void BuildEdgeStarted(const Edge* edge, int64_t start_time_millis) override;
34  void BuildEdgeFinished(Edge* edge, int64_t start_time_millis,
35  int64_t end_time_millis, ExitStatus exit_code,
36  const std::string& output) override;
37  void BuildStarted() override;
38  void BuildFinished() override;
39 
40  void Info(const char* msg, ...) override;
41  void Warning(const char* msg, ...) override;
42  void Error(const char* msg, ...) override;
43 
44  /// Format the progress status string by replacing the placeholders.
45  /// See the user manual for more information about the available
46  /// placeholders.
47  /// @param progress_status_format The format of the progress status.
48  /// @param status The status of the edge.
49  std::string FormatProgressStatus(const char* progress_status_format,
50  int64_t time_millis) const;
51 
52  /// Set the |explanations_| pointer. Used to implement `-d explain`.
53  void SetExplanations(Explanations* explanations) override {
54  explanations_ = explanations;
55  }
56 
57  private:
58  void PrintStatus(const Edge* edge, int64_t time_millis);
59 
61 
63 
64  /// How much wall clock elapsed so far?
66 
67  /// How much cpu clock elapsed so far?
69 
70  /// What percentage of predicted total time have elapsed already?
72 
73  /// Out of all the edges, for how many do we know previous time?
75  /// And how much time did they all take?
77 
78  /// Out of all the non-finished edges, for how many do we know previous time?
80  /// And how much time will they all take?
82 
83  /// For how many edges we don't know the previous run time?
85 
87 
88  /// Prints progress output.
90 
91  /// An optional Explanations pointer, used to implement `-d explain`.
93 
94  /// The custom progress status format to use.
96 
97  template <size_t S>
98  void SnprintfRate(double rate, char (&buf)[S], const char* format) const {
99  if (rate == -1)
100  snprintf(buf, S, "?");
101  else
102  snprintf(buf, S, format, rate);
103  }
104 
106  SlidingRateInfo(int n) : rate_(-1), N(n), last_update_(-1) {}
107 
108  double rate() { return rate_; }
109 
110  void UpdateRate(int update_hint, int64_t time_millis) {
111  if (update_hint == last_update_)
112  return;
113  last_update_ = update_hint;
114 
115  if (times_.size() == N)
116  times_.pop();
117  times_.push(time_millis);
118  if (times_.back() != times_.front())
119  rate_ = times_.size() / ((times_.back() - times_.front()) / 1e3);
120  }
121 
122  private:
123  double rate_;
124  const size_t N;
125  std::queue<double> times_;
127  };
128 
130 };
ExitStatus
Definition: exit_status.h:27
Options (e.g. verbosity, parallelism) passed to a build.
Definition: build.h:176
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:175
A class used to record a list of explanation strings associated with a given 'item' pointer.
Definition: explanations.h:27
Prints lines of text, possibly overprinting previously printed lines if the terminal supports it.
Definition: line_printer.h:23
void UpdateRate(int update_hint, int64_t time_millis)
std::queue< double > times_
Implementation of the Status interface that prints the status as human-readable strings to stdout.
void PrintStatus(const Edge *edge, int64_t time_millis)
void BuildEdgeStarted(const Edge *edge, int64_t start_time_millis) override
int64_t cpu_time_millis_
How much cpu clock elapsed so far?
void Warning(const char *msg,...) override
void BuildFinished() override
SlidingRateInfo current_rate_
LinePrinter printer_
Prints progress output.
void Error(const char *msg,...) override
void EdgeAddedToPlan(const Edge *edge) override
Callbacks for the Plan to notify us about adding/removing Edge's.
void EdgeRemovedFromPlan(const Edge *edge) override
int64_t time_millis_
How much wall clock elapsed so far?
const char * progress_status_format_
The custom progress status format to use.
void BuildEdgeFinished(Edge *edge, int64_t start_time_millis, int64_t end_time_millis, ExitStatus exit_code, const std::string &output) override
void SnprintfRate(double rate, char(&buf)[S], const char *format) const
int eta_predictable_edges_total_
Out of all the edges, for how many do we know previous time?
void SetExplanations(Explanations *explanations) override
Set the |explanations_| pointer. Used to implement -d explain.
int eta_unpredictable_edges_remaining_
For how many edges we don't know the previous run time?
void Info(const char *msg,...) override
int64_t eta_predictable_cpu_time_total_millis_
And how much time did they all take?
void RecalculateProgressPrediction()
StatusPrinter(const BuildConfig &config)
int eta_predictable_edges_remaining_
Out of all the non-finished edges, for how many do we know previous time?
void BuildStarted() override
const BuildConfig & config_
int64_t eta_predictable_cpu_time_remaining_millis_
And how much time will they all take?
std::string FormatProgressStatus(const char *progress_status_format, int64_t time_millis) const
Format the progress status string by replacing the placeholders.
double time_predicted_percentage_
What percentage of predicted total time have elapsed already?
Explanations * explanations_
An optional Explanations pointer, used to implement -d explain.
Abstract interface to object that tracks the status of a build: completion fraction,...
Definition: status.h:27
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28