Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
progress-updater.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
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 
15 #include "util/progress-updater.h"
16 
17 #include "common/logging.h"
18 #include <sstream>
19 
20 #include "common/names.h"
21 
22 using namespace impala;
23 
24 ProgressUpdater::ProgressUpdater(const string& label, int64_t total, int period) :
25  label_(label), logging_level_(2), total_(total), update_period_(period),
26  num_complete_(0), last_output_percentage_(0) {
27 }
28 
30  logging_level_(2), total_(0), update_period_(0),
31  num_complete_(0), last_output_percentage_(0) {
32 }
33 
34 void ProgressUpdater::Update(int64_t delta) {
35  DCHECK_GE(delta, 0);
36  if (delta == 0) return;
37 
38  num_complete_ += delta;
39 
40  // Cache some shared variables to avoid locking. It's possible the progress
41  // update is out of order (e.g. prints 1 out of 10 after 2 out of 10)
42  double old_percentage = last_output_percentage_;
43  int64_t num_complete = num_complete_;
44 
45  if (num_complete >= total_) {
46  // Always print the final 100% complete
47  VLOG(logging_level_) << label_ << " 100\% Complete ("
48  << num_complete << " out of " << total_ << ")";
49  return;
50  }
51 
52  // Convert to percentage as int
53  int new_percentage = (static_cast<double>(num_complete) / total_) * 100;
54  if (new_percentage - old_percentage > update_period_) {
55  // Only update shared variable if this guy was the latest.
56  __sync_val_compare_and_swap(&last_output_percentage_, old_percentage, new_percentage);
57  VLOG(logging_level_) << label_ << ": " << new_percentage << "\% Complete ("
58  << num_complete << " out of " << total_ << ")";
59  }
60 }
61 
62 string ProgressUpdater::ToString() const {
63  stringstream ss;
64  int64_t num_complete = num_complete_;
65  if (num_complete >= total_) {
66  // Always print the final 100% complete
67  ss << label_ << " 100\% Complete (" << num_complete << " out of " << total_ << ")";
68  return ss.str();
69  }
70  int percentage = (static_cast<double>(num_complete) / total_) * 100;
71  ss << label_ << ": " << percentage << "\% Complete ("
72  << num_complete << " out of " << total_ << ")";
73  return ss.str();
74 }
int64_t num_complete() const
std::string ToString() const
Returns a string representation of the current progress.
AtomicInt< int64_t > num_complete_
void Update(int64_t delta)