Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/performance_monitor/database.h" | 5 #include "chrome/browser/performance_monitor/database.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 } | 268 } |
| 269 scoped_ptr<Event> event = | 269 scoped_ptr<Event> event = |
| 270 Event::FromValue(scoped_ptr<base::DictionaryValue>(dict)); | 270 Event::FromValue(scoped_ptr<base::DictionaryValue>(dict)); |
| 271 if (!event.get()) | 271 if (!event.get()) |
| 272 continue; | 272 continue; |
| 273 events.push_back(linked_ptr<Event>(event.release())); | 273 events.push_back(linked_ptr<Event>(event.release())); |
| 274 } | 274 } |
| 275 return events; | 275 return events; |
| 276 } | 276 } |
| 277 | 277 |
| 278 Database::EventTypeSet Database::GetEventTypes(const base::Time& start, | 278 std::set<EventType> Database::GetEventTypes(const base::Time& start, |
| 279 const base::Time& end) { | 279 const base::Time& end) { |
| 280 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 280 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 281 EventTypeSet results; | 281 std::set<EventType> results; |
| 282 std::string start_key = CreateEventKey(start, EVENT_UNDEFINED); | 282 std::string start_key = CreateEventKey(start, EVENT_UNDEFINED); |
| 283 std::string end_key = CreateEventKey(end, EVENT_NUMBER_OF_EVENTS); | 283 std::string end_key = CreateEventKey(end, EVENT_NUMBER_OF_EVENTS); |
| 284 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_)); | 284 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_)); |
| 285 for (it->Seek(start_key); | 285 for (it->Seek(start_key); |
| 286 it->Valid() && it->key().ToString() <= end_key; | 286 it->Valid() && it->key().ToString() <= end_key; |
| 287 it->Next()) { | 287 it->Next()) { |
| 288 EventType key_type = EventKeyToEventType(it->key().ToString()); | 288 EventType key_type = EventKeyToEventType(it->key().ToString()); |
| 289 results.insert(key_type); | 289 results.insert(key_type); |
| 290 } | 290 } |
| 291 return results; | 291 return results; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 304 if (old_it != recent_map_.end()) | 304 if (old_it != recent_map_.end()) |
| 305 recent_db_->Delete(write_options_, old_it->second); | 305 recent_db_->Delete(write_options_, old_it->second); |
| 306 recent_map_[recent_map_key] = recent_key; | 306 recent_map_[recent_map_key] = recent_key; |
| 307 leveldb::Status recent_status = | 307 leveldb::Status recent_status = |
| 308 recent_db_->Put(write_options_, recent_key, value); | 308 recent_db_->Put(write_options_, recent_key, value); |
| 309 leveldb::Status metric_status = | 309 leveldb::Status metric_status = |
| 310 metric_db_->Put(write_options_, metric_key, value); | 310 metric_db_->Put(write_options_, metric_key, value); |
| 311 return recent_status.ok() && metric_status.ok(); | 311 return recent_status.ok() && metric_status.ok(); |
| 312 } | 312 } |
| 313 | 313 |
| 314 std::vector<const MetricDetails*> Database::GetActiveMetrics( | 314 std::set<MetricType> Database::GetActiveMetrics( |
|
eaugusti
2012/08/17 21:06:04
typedef std::set<MetricType> MetricTypeSet;
| |
| 315 const base::Time& start, const base::Time& end) { | 315 const base::Time& start, const base::Time& end) { |
| 316 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 316 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 317 std::vector<const MetricDetails*> results; | |
| 318 std::string recent_start_key = CreateRecentKey( | 317 std::string recent_start_key = CreateRecentKey( |
| 319 start, static_cast<MetricType>(0), std::string()); | 318 start, static_cast<MetricType>(0), std::string()); |
| 320 std::string recent_end_key = CreateRecentKey( | 319 std::string recent_end_key = CreateRecentKey( |
| 321 end, METRIC_NUMBER_OF_METRICS, std::string()); | 320 end, METRIC_NUMBER_OF_METRICS, std::string()); |
| 322 std::string recent_end_of_time_key = CreateRecentKey( | 321 std::string recent_end_of_time_key = CreateRecentKey( |
| 323 clock_->GetTime(), METRIC_NUMBER_OF_METRICS, std::string()); | 322 clock_->GetTime(), METRIC_NUMBER_OF_METRICS, std::string()); |
| 324 | 323 |
| 325 std::set<MetricType> active_metrics; | 324 std::set<MetricType> active_metrics; |
| 326 // Get all the guaranteed metrics. | 325 // Get all the guaranteed metrics. |
| 327 scoped_ptr<leveldb::Iterator> recent_it( | 326 scoped_ptr<leveldb::Iterator> recent_it( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 351 std::string metric_start_key = CreateMetricKey(start, *possible_it, | 350 std::string metric_start_key = CreateMetricKey(start, *possible_it, |
| 352 std::string()); | 351 std::string()); |
| 353 std::string metric_end_key = CreateMetricKey(end, *possible_it, | 352 std::string metric_end_key = CreateMetricKey(end, *possible_it, |
| 354 std::string()); | 353 std::string()); |
| 355 metric_it->Seek(metric_start_key); | 354 metric_it->Seek(metric_start_key); |
| 356 // Stats in the timerange from any activity makes the metric active. | 355 // Stats in the timerange from any activity makes the metric active. |
| 357 if (metric_it->Valid() && metric_it->key().ToString() <= metric_end_key) { | 356 if (metric_it->Valid() && metric_it->key().ToString() <= metric_end_key) { |
| 358 active_metrics.insert(*possible_it); | 357 active_metrics.insert(*possible_it); |
| 359 } | 358 } |
| 360 } | 359 } |
| 361 std::set<MetricType>::iterator it; | |
| 362 for (it = active_metrics.begin(); it != active_metrics.end(); ++it) | |
| 363 results.push_back(GetMetricDetails(*it)); | |
| 364 | 360 |
| 365 return results; | 361 return active_metrics; |
| 366 } | 362 } |
| 367 | 363 |
| 368 std::vector<std::string> Database::GetActiveActivities( | 364 std::set<std::string> Database::GetActiveActivities(MetricType metric_type, |
| 369 MetricType metric_type, const base::Time& start) { | 365 const base::Time& start) { |
| 370 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 366 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 371 std::vector<std::string> results; | 367 std::set<std::string> results; |
| 372 std::string start_key = CreateRecentKey( | 368 std::string start_key = CreateRecentKey( |
| 373 start, static_cast<MetricType>(0), std::string()); | 369 start, static_cast<MetricType>(0), std::string()); |
| 374 scoped_ptr<leveldb::Iterator> it(recent_db_->NewIterator(read_options_)); | 370 scoped_ptr<leveldb::Iterator> it(recent_db_->NewIterator(read_options_)); |
| 375 for (it->Seek(start_key); it->Valid(); it->Next()) { | 371 for (it->Seek(start_key); it->Valid(); it->Next()) { |
| 376 RecentKey split_key = SplitRecentKey(it->key().ToString()); | 372 RecentKey split_key = SplitRecentKey(it->key().ToString()); |
| 377 if (split_key.type == metric_type) | 373 if (split_key.type == metric_type) |
| 378 results.push_back(split_key.activity); | 374 results.insert(split_key.activity); |
| 379 } | 375 } |
| 380 return results; | 376 return results; |
| 381 } | 377 } |
| 382 | 378 |
| 383 bool Database::GetRecentStatsForActivityAndMetric( | 379 bool Database::GetRecentStatsForActivityAndMetric( |
| 384 const std::string& activity, | 380 const std::string& activity, |
| 385 MetricType metric, | 381 MetricType metric, |
| 386 MetricInfo* info) { | 382 Metric* info) { |
| 387 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 383 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 388 std::string recent_map_key = CreateRecentMapKey(metric, activity); | 384 std::string recent_map_key = CreateRecentMapKey(metric, activity); |
| 389 if (!ContainsKey(recent_map_, recent_map_key)) | 385 if (!ContainsKey(recent_map_, recent_map_key)) |
| 390 return false; | 386 return false; |
| 391 std::string recent_key = recent_map_[recent_map_key]; | 387 std::string recent_key = recent_map_[recent_map_key]; |
| 392 | 388 |
| 393 std::string result; | 389 std::string result; |
| 394 leveldb::Status status = recent_db_->Get(read_options_, recent_key, &result); | 390 leveldb::Status status = recent_db_->Get(read_options_, recent_key, &result); |
| 395 if (status.ok()) | 391 if (status.ok()) |
| 396 *info = MetricInfo(SplitRecentKey(recent_key).time, result); | 392 *info = Metric(SplitRecentKey(recent_key).time, result); |
| 397 return status.ok(); | 393 return status.ok(); |
| 398 } | 394 } |
| 399 | 395 |
| 400 Database::MetricInfoVector Database::GetStatsForActivityAndMetric( | 396 std::vector<Metric> Database::GetStatsForActivityAndMetric( |
| 401 const std::string& activity, MetricType metric_type, | 397 const std::string& activity, MetricType metric_type, |
| 402 const base::Time& start, const base::Time& end) { | 398 const base::Time& start, const base::Time& end) { |
| 403 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 399 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 404 MetricInfoVector results; | 400 std::vector<Metric> results; |
| 405 std::string start_key = CreateMetricKey(start, metric_type, activity); | 401 std::string start_key = CreateMetricKey(start, metric_type, activity); |
| 406 std::string end_key = CreateMetricKey(end, metric_type, activity); | 402 std::string end_key = CreateMetricKey(end, metric_type, activity); |
| 407 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); | 403 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); |
| 408 for (it->Seek(start_key); | 404 for (it->Seek(start_key); |
| 409 it->Valid() && it->key().ToString() <= end_key; | 405 it->Valid() && it->key().ToString() <= end_key; |
| 410 it->Next()) { | 406 it->Next()) { |
| 411 MetricKey split_key = SplitMetricKey(it->key().ToString()); | 407 MetricKey split_key = SplitMetricKey(it->key().ToString()); |
| 412 if (split_key.activity == activity) | 408 if (split_key.activity == activity) |
| 413 results.push_back(MetricInfo(split_key.time, it->value().ToString())); | 409 results.push_back(Metric(split_key.time, it->value().ToString())); |
| 414 } | 410 } |
| 415 return results; | 411 return results; |
| 416 } | 412 } |
| 417 | 413 |
| 418 Database::MetricVectorMap Database::GetStatsForMetricByActivity( | 414 Database::MetricVectorMap Database::GetStatsForMetricByActivity( |
| 419 MetricType metric_type, const base::Time& start, const base::Time& end) { | 415 MetricType metric_type, const base::Time& start, const base::Time& end) { |
| 420 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 416 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 421 MetricVectorMap results; | 417 MetricVectorMap results; |
| 422 std::string start_key = CreateMetricKey(start, metric_type, std::string()); | 418 std::string start_key = CreateMetricKey(start, metric_type, std::string()); |
| 423 std::string end_key = CreateMetricKey(end, metric_type, std::string()); | 419 std::string end_key = CreateMetricKey(end, metric_type, std::string()); |
| 424 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); | 420 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); |
| 425 for (it->Seek(start_key); | 421 for (it->Seek(start_key); |
| 426 it->Valid() && it->key().ToString() <= end_key; | 422 it->Valid() && it->key().ToString() <= end_key; |
| 427 it->Next()) { | 423 it->Next()) { |
| 428 MetricKey split_key = SplitMetricKey(it->key().ToString()); | 424 MetricKey split_key = SplitMetricKey(it->key().ToString()); |
| 429 if (!results[split_key.activity].get()) { | 425 if (!results[split_key.activity].get()) { |
| 430 results[split_key.activity] = | 426 results[split_key.activity] = |
| 431 linked_ptr<MetricInfoVector>(new MetricInfoVector()); | 427 linked_ptr<std::vector<Metric> >(new std::vector<Metric>()); |
| 432 } | 428 } |
| 433 results[split_key.activity]->push_back( | 429 results[split_key.activity]->push_back( |
| 434 MetricInfo(split_key.time, it->value().ToString())); | 430 Metric(split_key.time, it->value().ToString())); |
| 435 } | 431 } |
| 436 return results; | 432 return results; |
| 437 } | 433 } |
| 438 | 434 |
| 439 Database::Database(const FilePath& path) | 435 Database::Database(const FilePath& path) |
| 440 : path_(path), | 436 : path_(path), |
| 441 read_options_(leveldb::ReadOptions()), | 437 read_options_(leveldb::ReadOptions()), |
| 442 write_options_(leveldb::WriteOptions()) { | 438 write_options_(leveldb::WriteOptions()) { |
| 443 InitDBs(); | 439 InitDBs(); |
| 444 LoadRecents(); | 440 LoadRecents(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 start_time_key_ = CreateActiveIntervalKey(current_time); | 519 start_time_key_ = CreateActiveIntervalKey(current_time); |
| 524 end_time = start_time_key_; | 520 end_time = start_time_key_; |
| 525 } else { | 521 } else { |
| 526 end_time = CreateActiveIntervalKey(clock_->GetTime()); | 522 end_time = CreateActiveIntervalKey(clock_->GetTime()); |
| 527 } | 523 } |
| 528 last_update_time_ = current_time; | 524 last_update_time_ = current_time; |
| 529 active_interval_db_->Put(write_options_, start_time_key_, end_time); | 525 active_interval_db_->Put(write_options_, start_time_key_, end_time); |
| 530 } | 526 } |
| 531 | 527 |
| 532 } // namespace performance_monitor | 528 } // namespace performance_monitor |
| OLD | NEW |