memory_dump_scheduler.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/trace_event/memory_dump_request_args.h"
  11. namespace base {
  12. class SequencedTaskRunner;
  13. namespace trace_event {
  14. // Schedules global dump requests based on the triggers added. The methods of
  15. // this class are NOT thread safe and the client has to take care of invoking
  16. // all the methods of the class safely.
  17. class BASE_EXPORT MemoryDumpScheduler {
  18. public:
  19. using PeriodicCallback = RepeatingCallback<void(MemoryDumpLevelOfDetail)>;
  20. // Passed to Start().
  21. struct BASE_EXPORT Config {
  22. struct Trigger {
  23. MemoryDumpLevelOfDetail level_of_detail;
  24. uint32_t period_ms;
  25. };
  26. Config();
  27. Config(const Config&);
  28. ~Config();
  29. std::vector<Trigger> triggers;
  30. PeriodicCallback callback;
  31. };
  32. static MemoryDumpScheduler* GetInstance();
  33. MemoryDumpScheduler(const MemoryDumpScheduler&) = delete;
  34. MemoryDumpScheduler& operator=(const MemoryDumpScheduler&) = delete;
  35. void Start(Config, scoped_refptr<SequencedTaskRunner> task_runner);
  36. void Stop();
  37. bool is_enabled_for_testing() const { return bool(task_runner_); }
  38. private:
  39. friend class MemoryDumpSchedulerTest;
  40. MemoryDumpScheduler();
  41. ~MemoryDumpScheduler();
  42. void StartInternal(Config);
  43. void StopInternal();
  44. void Tick(uint32_t expected_generation);
  45. // Accessed only by the public methods (never from the task runner itself).
  46. scoped_refptr<SequencedTaskRunner> task_runner_;
  47. // These fields instead are only accessed from within the task runner.
  48. uint32_t period_ms_ = 0; // 0 == disabled.
  49. // Used to invalidate outstanding tasks after Stop().
  50. uint32_t generation_ = 0;
  51. uint32_t tick_count_;
  52. uint32_t light_dump_rate_;
  53. uint32_t heavy_dump_rate_;
  54. PeriodicCallback callback_;
  55. };
  56. } // namespace trace_event
  57. } // namespace base
  58. #endif // BASE_TRACE_EVENT_MEMORY_DUMP_SCHEDULER_H_