2828 * @copyright 2022, Catalyst IT
2929 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3030 */
31- class cron_processor implements processor {
31+ class cron_processor extends processor {
3232 /** @var float Timestamp updated after processing each sample */
3333 public $ sampletime ;
3434
35- /** @var sample_set A sample set recorded while processing a task */
36- public $ tasksampleset = null ;
37-
38- /** @var sample_set A sample set for memory usage recorded while processing a task */
39- protected $ memoryusagesampleset ;
40-
41- /** @var int */
42- protected $ samplingperiod ;
43-
44- /** @var int */
45- protected $ samplelimit ;
46-
47- /** @var int */
48- protected $ maxsamples ;
49-
50- /** @var int */
51- protected $ logcount = 0 ;
52-
53- /** @var bool */
54- protected static $ alreadyprofiling = false ;
55-
56- /** @var array */
57- protected static $ logs = [];
35+ /**
36+ * Construct the web processor.
37+ */
38+ public function __construct () {
39+ // Preload config values to avoid DB access during processing. See manager::get_altconnection() for more information.
40+ parent ::__construct ();
41+ $ this ->minduration = (float ) get_config ('tool_excimer ' , 'task_min_duration ' );
42+ }
5843
5944 /**
6045 * Initialises the processor
@@ -64,10 +49,7 @@ class cron_processor implements processor {
6449 public function init (manager $ manager ) {
6550 $ this ->sampletime = $ manager ->get_starttime ();
6651
67- // Preload config values to avoid DB access during processing. See manager::get_altconnection() for more information.
68- $ this ->samplingperiod = script_metadata::get_sampling_period ();
69- $ this ->samplelimit = script_metadata::get_sample_limit ();
70- $ this ->maxsamples = script_metadata::get_max_samples ();
52+ // The callback is triggered when the number of samples reach the maxsamples and when profiler is destroyed.
7153 $ manager ->get_profiler ()->setFlushCallback (function ($ log ) use ($ manager ) {
7254 $ this ->on_interval ($ log , $ manager );
7355 }, $ this ->maxsamples );
@@ -84,7 +66,7 @@ function () use ($manager) {
8466
8567 /**
8668 * Get sampling period of processor
87- *
69+ * (used for testing)
8870 * @return int sampling period
8971 */
9072 public function get_sampling_period () {
@@ -93,35 +75,13 @@ public function get_sampling_period() {
9375
9476 /**
9577 * Get sample limit of processor
96- *
78+ * (used for testing)
9779 * @return int sample_limit
9880 */
9981 public function get_sample_limit () {
10082 return $ this ->samplelimit ;
10183 }
10284
103- /**
104- * Gets the minimum duration required for a profile to be saved, as seconds.
105- *
106- * @return float
107- * @throws \dml_exception
108- */
109- public function get_min_duration (): float {
110- return (float ) get_config ('tool_excimer ' , 'task_min_duration ' );
111- }
112-
113- /**
114- * Doubling the sampling period when we reach the samples limit
115- *
116- * @param manager $manager
117- */
118- public function on_reach_limit (manager $ manager ) {
119- $ this ->samplingperiod *= 2 ;
120- // This will take effect the next time start() is called.
121- $ manager ->get_profiler ()->setPeriod ($ this ->samplingperiod );
122- $ manager ->get_profiler ()->start ();
123- }
124-
12585 /**
12686 * Examines a sample generated by the profiler.
12787 *
@@ -246,7 +206,7 @@ public function process(manager $manager, float $finishtime): profile {
246206 $ profile ->set ('flamedatad3 ' , flamed3_node::from_sample_set_samples ($ this ->tasksampleset ->samples ));
247207 $ profile ->set ('numsamples ' , count ($ this ->tasksampleset ->samples ));
248208 $ profile ->set ('numevents ' , $ this ->tasksampleset ->count ());
249- $ profile ->set ('samplerate ' , $ this -> samplingperiod * 1000 );
209+ $ profile ->set ('samplerate ' , ( int ) (( $ duration * 1000 ) / $ this -> tasksampleset -> count ()) );
250210 $ profile ->save_record ();
251211 }
252212 return $ profile ;
0 commit comments