• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ext/filters/client_channel/lb_policy/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 namespace grpc_core {
29 
AddCallStarted()30 void GrpcLbClientStats::AddCallStarted() {
31   gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
32 }
33 
AddCallFinished(bool finished_with_client_failed_to_send,bool finished_known_received)34 void GrpcLbClientStats::AddCallFinished(
35     bool finished_with_client_failed_to_send, bool finished_known_received) {
36   gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
37   if (finished_with_client_failed_to_send) {
38     gpr_atm_full_fetch_add(&num_calls_finished_with_client_failed_to_send_,
39                            (gpr_atm)1);
40   }
41   if (finished_known_received) {
42     gpr_atm_full_fetch_add(&num_calls_finished_known_received_, (gpr_atm)1);
43   }
44 }
45 
AddCallDroppedLocked(char * token)46 void GrpcLbClientStats::AddCallDroppedLocked(char* token) {
47   // Increment num_calls_started and num_calls_finished.
48   gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
49   gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
50   // Record the drop.
51   if (drop_token_counts_ == nullptr) {
52     drop_token_counts_.reset(New<DroppedCallCounts>());
53   }
54   for (size_t i = 0; i < drop_token_counts_->size(); ++i) {
55     if (strcmp((*drop_token_counts_)[i].token.get(), token) == 0) {
56       ++(*drop_token_counts_)[i].count;
57       return;
58     }
59   }
60   // Not found, so add a new entry.
61   drop_token_counts_->emplace_back(UniquePtr<char>(gpr_strdup(token)), 1);
62 }
63 
64 namespace {
65 
AtomicGetAndResetCounter(int64_t * value,gpr_atm * counter)66 void AtomicGetAndResetCounter(int64_t* value, gpr_atm* counter) {
67   *value = static_cast<int64_t>(gpr_atm_full_xchg(counter, (gpr_atm)0));
68 }
69 
70 }  // namespace
71 
GetLocked(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,UniquePtr<DroppedCallCounts> * drop_token_counts)72 void GrpcLbClientStats::GetLocked(
73     int64_t* num_calls_started, int64_t* num_calls_finished,
74     int64_t* num_calls_finished_with_client_failed_to_send,
75     int64_t* num_calls_finished_known_received,
76     UniquePtr<DroppedCallCounts>* drop_token_counts) {
77   AtomicGetAndResetCounter(num_calls_started, &num_calls_started_);
78   AtomicGetAndResetCounter(num_calls_finished, &num_calls_finished_);
79   AtomicGetAndResetCounter(num_calls_finished_with_client_failed_to_send,
80                            &num_calls_finished_with_client_failed_to_send_);
81   AtomicGetAndResetCounter(num_calls_finished_known_received,
82                            &num_calls_finished_known_received_);
83   *drop_token_counts = std::move(drop_token_counts_);
84 }
85 
86 }  // namespace grpc_core
87