| 154 | static CollectorSpeedLimit g_null_speed_limit = BVAR_COLLECTOR_SPEED_LIMIT_INITIALIZER; |
| 155 | |
| 156 | void Collector::grab_thread() { |
| 157 | _last_active_cpuwide_us = butil::cpuwide_time_us(); |
| 158 | int64_t last_before_update_sl = _last_active_cpuwide_us; |
| 159 | |
| 160 | // This is the thread for collecting TLS submissions. User's callbacks are |
| 161 | // called inside the separate _dump_thread to prevent a slow callback |
| 162 | // (caused by busy disk generally) from blocking collecting code too long |
| 163 | // that pending requests may explode memory. |
| 164 | CHECK_EQ(0, pthread_create(&_dump_thread, NULL, run_dump_thread, this)); |
| 165 | |
| 166 | // vars |
| 167 | bvar::PassiveStatus<int64_t> pending_sampled_data( |
| 168 | "bvar_collector_pending_samples", get_pending_count, this); |
| 169 | double busy_seconds = 0; |
| 170 | bvar::PassiveStatus<double> busy_seconds_var(deref_value<double>, &busy_seconds); |
| 171 | bvar::PerSecond<bvar::PassiveStatus<double> > busy_seconds_second( |
| 172 | "bvar_collector_grab_thread_usage", &busy_seconds_var); |
| 173 | |
| 174 | bvar::PassiveStatus<int64_t> ngrab_var(deref_value<int64_t>, &_ngrab); |
| 175 | bvar::PerSecond<bvar::PassiveStatus<int64_t> > ngrab_second( |
| 176 | "bvar_collector_grab_second", &ngrab_var); |
| 177 | |
| 178 | // Maps for calculating speed limit. |
| 179 | typedef std::map<CollectorSpeedLimit*, size_t> GrapMap; |
| 180 | GrapMap last_ngrab_map; |
| 181 | GrapMap ngrab_map; |
| 182 | // Map for group samples by preprocessors. |
| 183 | typedef std::map<CollectorPreprocessor*, std::vector<Collected*> > |
| 184 | PreprocessorMap; |
| 185 | PreprocessorMap prep_map; |
| 186 | |
| 187 | // The main loop. |
| 188 | while (!_stop) { |
| 189 | const int64_t abstime = _last_active_cpuwide_us + COLLECTOR_GRAB_INTERVAL_US; |
| 190 | |
| 191 | // Clear and reuse vectors in prep_map, don't clear prep_map directly. |
| 192 | for (PreprocessorMap::iterator it = prep_map.begin(); it != prep_map.end(); |
| 193 | ++it) { |
| 194 | it->second.clear(); |
| 195 | } |
| 196 | |
| 197 | // Collect TLS submissions and give them to dump_thread. |
| 198 | butil::LinkNode<Collected>* head = this->reset(); |
| 199 | if (head) { |
| 200 | butil::LinkNode<Collected> tmp_root; |
| 201 | head->InsertBeforeAsList(&tmp_root); |
| 202 | head = NULL; |
| 203 | |
| 204 | // Group samples by preprocessors. |
| 205 | for (butil::LinkNode<Collected>* p = tmp_root.next(); p != &tmp_root;) { |
| 206 | butil::LinkNode<Collected>* saved_next = p->next(); |
| 207 | p->RemoveFromList(); |
| 208 | CollectorPreprocessor* prep = p->value()->preprocessor(); |
| 209 | prep_map[prep].push_back(p->value()); |
| 210 | p = saved_next; |
| 211 | } |
| 212 | // Iterate prep_map |
| 213 | butil::LinkNode<Collected> root; |
no test coverage detected