memory_dump_scheduler.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H
  5. #define BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "base/base_export.h"
  9. #include "base/callback.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/trace_event/memory_dump_request_args.h"
  12. namespace base {
  13. class SequencedTaskRunner;
  14. namespace trace_event {
  15. // Schedules global dump requests based on the triggers added. The methods of
  16. // this class are NOT thread safe and the client has to take care of invoking
  17. // all the methods of the class safely.
  18. class BASE_EXPORT MemoryDumpScheduler {
  19. public:
  20. using PeriodicCallback = RepeatingCallback<void(MemoryDumpLevelOfDetail)>;
  21. // Passed to Start().
  22. struct BASE_EXPORT Config {
  23. struct Trigger {
  24. MemoryDumpLevelOfDetail level_of_detail;
  25. uint32_t period_ms;
  26. };
  27. Config();
  28. Config(const Config&);
  29. ~Config();
  30. std::vector<Trigger> triggers;
  31. PeriodicCallback callback;
  32. };
  33. static MemoryDumpScheduler* GetInstance();
  34. void Start(Config, scoped_refptr<SequencedTaskRunner> task_runner);
  35. void Stop();
  36. bool is_enabled_for_testing() const { return bool(task_runner_); }
  37. private:
  38. friend class MemoryDumpSchedulerTest;
  39. MemoryDumpScheduler();
  40. ~MemoryDumpScheduler();
  41. void StartInternal(Config);
  42. void StopInternal();
  43. void Tick(uint32_t expected_generation);
  44. // Accessed only by the public methods (never from the task runner itself).
  45. scoped_refptr<SequencedTaskRunner> task_runner_;
  46. // These fields instead are only accessed from within the task runner.
  47. uint32_t period_ms_; // 0 == disabled.
  48. uint32_t generation_; // Used to invalidate outstanding tasks after Stop().
  49. uint32_t tick_count_;
  50. uint32_t light_dump_rate_;
  51. uint32_t heavy_dump_rate_;
  52. PeriodicCallback callback_;
  53. DISALLOW_COPY_AND_ASSIGN(MemoryDumpScheduler);
  54. };
  55. } // namespace trace_event
  56. } // namespace base
  57. #endif // BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H