1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/load_balancing/grpclb/grpclb_client_stats.h"
22
23 #include <string.h>
24
25 #include <grpc/support/atm.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/lib/gprpp/sync.h"
29
30 namespace grpc_core {
31
AddCallStarted()32 void GrpcLbClientStats::AddCallStarted() {
33 gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
34 }
35
AddCallFinished(bool finished_with_client_failed_to_send,bool finished_known_received)36 void GrpcLbClientStats::AddCallFinished(
37 bool finished_with_client_failed_to_send, bool finished_known_received) {
38 gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
39 if (finished_with_client_failed_to_send) {
40 gpr_atm_full_fetch_add(&num_calls_finished_with_client_failed_to_send_,
41 (gpr_atm)1);
42 }
43 if (finished_known_received) {
44 gpr_atm_full_fetch_add(&num_calls_finished_known_received_, (gpr_atm)1);
45 }
46 }
47
AddCallDropped(const char * token)48 void GrpcLbClientStats::AddCallDropped(const char* token) {
49 // Increment num_calls_started and num_calls_finished.
50 gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
51 gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
52 // Record the drop.
53 MutexLock lock(&drop_count_mu_);
54 if (drop_token_counts_ == nullptr) {
55 drop_token_counts_ = std::make_unique<DroppedCallCounts>();
56 }
57 for (size_t i = 0; i < drop_token_counts_->size(); ++i) {
58 if (strcmp((*drop_token_counts_)[i].token.get(), token) == 0) {
59 ++(*drop_token_counts_)[i].count;
60 return;
61 }
62 }
63 // Not found, so add a new entry.
64 drop_token_counts_->emplace_back(UniquePtr<char>(gpr_strdup(token)), 1);
65 }
66
67 namespace {
68
AtomicGetAndResetCounter(int64_t * value,gpr_atm * counter)69 void AtomicGetAndResetCounter(int64_t* value, gpr_atm* counter) {
70 *value = static_cast<int64_t>(gpr_atm_full_xchg(counter, (gpr_atm)0));
71 }
72
73 } // namespace
74
Get(int64_t * num_calls_started,int64_t * num_calls_finished,int64_t * num_calls_finished_with_client_failed_to_send,int64_t * num_calls_finished_known_received,std::unique_ptr<DroppedCallCounts> * drop_token_counts)75 void GrpcLbClientStats::Get(
76 int64_t* num_calls_started, int64_t* num_calls_finished,
77 int64_t* num_calls_finished_with_client_failed_to_send,
78 int64_t* num_calls_finished_known_received,
79 std::unique_ptr<DroppedCallCounts>* drop_token_counts) {
80 AtomicGetAndResetCounter(num_calls_started, &num_calls_started_);
81 AtomicGetAndResetCounter(num_calls_finished, &num_calls_finished_);
82 AtomicGetAndResetCounter(num_calls_finished_with_client_failed_to_send,
83 &num_calls_finished_with_client_failed_to_send_);
84 AtomicGetAndResetCounter(num_calls_finished_known_received,
85 &num_calls_finished_known_received_);
86 MutexLock lock(&drop_count_mu_);
87 *drop_token_counts = std::move(drop_token_counts_);
88 }
89
90 } // namespace grpc_core
91