1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
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 <fb303/ExportedStatMap.h>
18 #include <fb303/TimeseriesExporter.h>
19
20 using folly::StringPiece;
21
22 namespace facebook {
23 namespace fb303 {
24
exportStat(StringPiece name,ExportType type,const ExportedStat * copyMe)25 void ExportedStatMap::exportStat(
26 StringPiece name,
27 ExportType type,
28 const ExportedStat* copyMe) {
29 StatPtr item = getStatPtrNoExport(name, nullptr, copyMe);
30 TimeseriesExporter::exportStat(item, type, name, dynamicCounters_);
31 }
32
getStatPtr(StringPiece name,const ExportType * exportType)33 ExportedStatMap::StatPtr ExportedStatMap::getStatPtr(
34 StringPiece name,
35 const ExportType* exportType) {
36 // find the stat
37 bool created = false;
38 StatPtr item = getStatPtrNoExport(name, &created);
39
40 if (created) {
41 // if newly created, add export types
42 if (nullptr != exportType) {
43 TimeseriesExporter::exportStat(item, *exportType, name, dynamicCounters_);
44 } else {
45 for (auto type : defaultTypes_) {
46 TimeseriesExporter::exportStat(item, type, name, dynamicCounters_);
47 }
48 }
49 }
50 return item;
51 }
52
getStatPtrNoExport(StringPiece name,bool * createdPtr,const ExportedStat * copyMe)53 ExportedStatMap::StatPtr ExportedStatMap::getStatPtrNoExport(
54 StringPiece name,
55 bool* createdPtr,
56 const ExportedStat* copyMe) {
57 if (createdPtr) {
58 *createdPtr = false;
59 }
60
61 {
62 auto rlock = statMap_.rlock();
63 auto iter = rlock->find(name);
64 if (iter != rlock->end()) {
65 return iter->second;
66 }
67 }
68
69 auto ulock = statMap_.ulock();
70 auto iter = ulock->find(name);
71 if (iter != ulock->end()) {
72 // Stat was populated before we acquired the ulock.
73 return iter->second;
74 }
75
76 auto value = std::make_shared<SyncStat>(defaultStat_);
77 if (copyMe) {
78 *value = *copyMe;
79 }
80
81 if (createdPtr) {
82 *createdPtr = true;
83 }
84
85 auto wlock = ulock.moveFromUpgradeToWrite();
86 auto item = wlock->try_emplace(name, std::move(value));
87 DCHECK(item.second);
88 return item.first->second;
89 }
90
unExportStatAll(StringPiece name)91 void ExportedStatMap::unExportStatAll(StringPiece name) {
92 // Get unlocked item as we will not access the value of the item
93 // And the function called on the value assume that they can access
94 // the value without locking
95 auto lockedStatMap = statMap_.wlock();
96 auto stat = lockedStatMap->find(name);
97 if (stat != lockedStatMap->end()) {
98 for (auto type : ExportTypeMeta::kExportTypes) {
99 TimeseriesExporter::unExportStat(
100 stat->second, type, name, dynamicCounters_);
101 }
102 lockedStatMap->erase(name);
103 }
104 }
105
forgetAllStats()106 void ExportedStatMap::forgetAllStats() {
107 statMap_.wlock()->clear();
108 }
109
forgetStatsFor(StringPiece name)110 void ExportedStatMap::forgetStatsFor(StringPiece name) {
111 statMap_.wlock()->erase(name);
112 }
113
clearAllStats()114 void ExportedStatMap::clearAllStats() {
115 auto lockedStatMap = statMap_.wlock();
116 for (auto& statPtrKvp : *lockedStatMap) {
117 statPtrKvp.second->lock()->clear();
118 }
119 }
120 } // namespace fb303
121 } // namespace facebook
122