File: fu-progress.c

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (1147 lines) | stat: -rw-r--r-- 29,419 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
/*
 * Copyright 2021 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#define G_LOG_DOMAIN "FuProgress"

#include "config.h"

#include <math.h>

#include "fu-progress-private.h"
#include "fu-string.h"

/**
 * FuProgress:
 *
 * Objects can use fu_progress_set_percentage() if the absolute percentage
 * is known. Percentages should always go up, not down.
 *
 * Modules usually set the number of steps that are expected using
 * fu_progress_set_steps() and then after each section is completed,
 * the fu_progress_step_done() function should be called. This will automatically
 * call fu_progress_set_percentage() with the correct values.
 *
 * #FuProgress allows sub-modules to be "chained up" to the parent module
 * so that as the sub-module progresses, so does the parent.
 * The child can be reused for each section, and chains can be deep.
 *
 * To get a child object, you should use [method@FuProgress.get_child]. and then
 * use the result in any sub-process. You should ensure that the child
 * is not reused without calling fu_progress_step_done().
 *
 * There are a few nice touches in this module, so that if a module only has
 * one progress step, the child progress is used for parent updates.
 *
 *    static void
 *    _do_something(FuProgress *self)
 *    {
 *       // setup correct number of steps
 *       fu_progress_set_steps(self, 2);
 *
 *       // run a sub function
 *       _do_something_else1(fu_progress_get_child(self));
 *
 *       // this section done
 *       fu_progress_step_done(self);
 *
 *       // run another sub function
 *       _do_something_else2(fu_progress_get_child(self));
 *
 *       // this progress done (all complete)
 *       fu_progress_step_done(self);
 *    }
 *
 * See also: [class@FuDevice]
 */

struct _FuProgress {
	GObject parent_instance;
	gchar *id;
	gchar *name;
	FuProgressFlags flags;
	guint percentage;
	FwupdStatus status;
	GPtrArray *children; /* of FuProgress */
	gboolean profile;
	gboolean any_child_has_step_weighting;
	gdouble duration; /* seconds */
	gdouble global_fraction;
	guint step_weighting;
	GTimer *timer;
	GTimer *timer_child;
	guint step_now;
	guint step_done;
	guint step_scaling;
	FuProgress *parent; /* no-ref */
};

enum { SIGNAL_PERCENTAGE_CHANGED, SIGNAL_STATUS_CHANGED, SIGNAL_LAST };

static guint signals[SIGNAL_LAST] = {0};

static void
fu_progress_codec_iface_init(FwupdCodecInterface *iface);

G_DEFINE_TYPE_WITH_CODE(FuProgress,
			fu_progress,
			G_TYPE_OBJECT,
			G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC, fu_progress_codec_iface_init))

#define FU_PROGRESS_STEPS_MAX 1000

/**
 * fu_progress_get_id:
 * @self: a #FuProgress
 *
 * Return the id of the progress, which is normally set by the caller.
 *
 * Returns: progress ID
 *
 * Since: 1.7.0
 **/
const gchar *
fu_progress_get_id(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), NULL);
	return self->id;
}

/**
 * fu_progress_set_id:
 * @self: a #FuProgress
 * @id: progress ID, normally `G_STRLOC`
 *
 * Sets the id of the progress.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_id(FuProgress *self, const gchar *id)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(id != NULL);

	/* not changed */
	if (g_strcmp0(self->id, id) == 0)
		return;

	/* set id */
	g_free(self->id);
	self->id = g_strdup(id);
}

/**
 * fu_progress_get_name:
 * @self: a #FuProgress
 *
 * Return the nice name of the progress, which is normally set by the caller.
 *
 * Returns: progress nice name, e.g. `add-devices`
 *
 * Since: 1.8.2
 **/
const gchar *
fu_progress_get_name(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), NULL);
	return self->name;
}

static const gchar *
fu_progress_get_name_fallback(FuProgress *self)
{
	if (self->name != NULL)
		return self->name;
	return fwupd_status_to_string(self->status);
}

/**
 * fu_progress_set_name:
 * @self: a #FuProgress
 * @name: progress nice name, e.g. `add-devices`, or perhaps just `G_STRFUNC`
 *
 * Sets the nice name of the progress.
 *
 * Since: 1.8.2
 **/
void
fu_progress_set_name(FuProgress *self, const gchar *name)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(name != NULL);

	/* not changed */
	if (g_strcmp0(self->name, name) == 0)
		return;

	/* set name */
	g_free(self->name);
	self->name = g_strdup(name);
}

/**
 * fu_progress_get_status:
 * @self: a #FuProgress
 *
 * Return the status of the progress, which is normally indirectly by fu_progress_add_step().
 *
 * Returns: status
 *
 * Since: 1.7.0
 **/
FwupdStatus
fu_progress_get_status(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), FWUPD_STATUS_UNKNOWN);
	return self->status;
}

/**
 * fu_progress_add_flag:
 * @self: a #FuProgress
 * @flag: an internal progress flag, e.g. %FU_PROGRESS_FLAG_GUESSED
 *
 * Adds a flag.
 *
 * Since: 1.7.0
 **/
void
fu_progress_add_flag(FuProgress *self, FuProgressFlags flag)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	self->flags |= flag;
}

/**
 * fu_progress_remove_flag:
 * @self: a #FuProgress
 * @flag: an internal progress flag, e.g. %FU_PROGRESS_FLAG_GUESSED
 *
 * Removes a flag.
 *
 * Since: 1.7.0
 **/
void
fu_progress_remove_flag(FuProgress *self, FuProgressFlags flag)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	self->flags &= ~flag;
}

/**
 * fu_progress_has_flag:
 * @self: a #FuProgress
 * @flag: an internal progress flag, e.g. %FU_PROGRESS_FLAG_GUESSED
 *
 * Tests for a flag.
 *
 * Since: 1.7.0
 **/
gboolean
fu_progress_has_flag(FuProgress *self, FuProgressFlags flag)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), FALSE);
	return (self->flags & flag) > 0;
}

/**
 * fu_progress_set_status:
 * @self: a #FuProgress
 * @status: device status
 *
 * Sets the status of the progress.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_status(FuProgress *self, FwupdStatus status)
{
	g_return_if_fail(FU_IS_PROGRESS(self));

	/* not changed */
	if (self->status == status)
		return;

	/* save */
	self->status = status;
	g_signal_emit(self, signals[SIGNAL_STATUS_CHANGED], 0, status);
}

/**
 * fu_progress_get_percentage:
 * @self: a #FuProgress
 *
 * Get the last set progress percentage.
 *
 * Return value: The percentage value, or %G_MAXUINT for error
 *
 * Since: 1.7.0
 **/
guint
fu_progress_get_percentage(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), G_MAXUINT);
	if (self->percentage == G_MAXUINT)
		return 0;
	return self->percentage;
}

static void
fu_progress_set_parent(FuProgress *self, FuProgress *parent)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(FU_IS_PROGRESS(parent));
	self->parent = parent; /* no ref! */
	self->profile = fu_progress_get_profile(parent);
}

/**
 * fu_progress_get_duration:
 * @self: a #FuProgress
 *
 * Get the duration of the step.
 *
 * Return value: The duration value in seconds
 *
 * Since: 1.8.2
 **/
gdouble
fu_progress_get_duration(FuProgress *self)
{
	return self->duration;
}

static void
fu_progress_set_duration(FuProgress *self, gdouble duration)
{
	self->duration = duration;
}

static void
fu_progress_build_parent_chain(FuProgress *self, GString *str, guint level)
{
	if (self->parent != NULL)
		fu_progress_build_parent_chain(self->parent, str, level + 1);
	g_string_append_printf(str,
			       "%u) %s (%u/%u)\n",
			       level,
			       self->id,
			       self->step_now,
			       self->children->len);
}

/**
 * fu_progress_set_percentage:
 * @self: a #FuProgress
 * @percentage: value between 0% and 100%
 *
 * Sets the progress percentage complete.
 *
 * NOTE: this must be above what was previously set, or it will be rejected.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_percentage(FuProgress *self, guint percentage)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(percentage <= 100);

	/* is it the same */
	if (percentage == self->percentage)
		return;

	/* is it less */
	if (self->percentage != G_MAXUINT && percentage < self->percentage) {
		if (self->profile) {
			g_autoptr(GString) str = g_string_new(NULL);
			fu_progress_build_parent_chain(self, str, 0);
			g_warning("percentage should not go down from %u to %u: %s",
				  self->percentage,
				  percentage,
				  str->str);
		}
		return;
	}

	/* done */
	if (percentage == 100) {
		fu_progress_set_duration(self, g_timer_elapsed(self->timer, NULL));
		for (guint i = 0; i < self->children->len; i++) {
			FuProgress *child = g_ptr_array_index(self->children, i);
			g_signal_handlers_disconnect_by_data(child, self);
		}
	}

	/* save */
	self->percentage = percentage;
	g_signal_emit(self, signals[SIGNAL_PERCENTAGE_CHANGED], 0, percentage);
}

/**
 * fu_progress_set_percentage_full:
 * @self: a #FuDevice
 * @progress_done: the bytes already done
 * @progress_total: the total number of bytes
 *
 * Sets the progress completion using the raw progress values.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_percentage_full(FuProgress *self, gsize progress_done, gsize progress_total)
{
	gdouble percentage = 0.f;
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(progress_done <= progress_total);
	if (progress_total > 0)
		percentage = (100.f * (gdouble)progress_done) / (gdouble)progress_total;
	fu_progress_set_percentage(self, (guint)percentage);
}

/**
 * fu_progress_set_profile:
 * @self: A #FuProgress
 * @profile: if profiling should be enabled
 *
 * This enables profiling of FuProgress. This may be useful in development,
 * but be warned; enabling profiling makes #FuProgress very slow.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_profile(FuProgress *self, gboolean profile)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	self->profile = profile;
}

/**
 * fu_progress_get_profile:
 * @self: A #FuProgress
 *
 * Returns if the profile is enabled for this progress.
 *
 * Return value: if profiling has been enabled
 *
 * Since: 1.8.2
 **/
gboolean
fu_progress_get_profile(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), FALSE);
	return self->profile;
}

/**
 * fu_progress_reset:
 * @self: A #FuProgress
 *
 * Resets the #FuProgress object to unset
 *
 * Since: 1.7.0
 **/
void
fu_progress_reset(FuProgress *self)
{
	g_return_if_fail(FU_IS_PROGRESS(self));

	/* reset values */
	self->step_now = 0;
	self->percentage = G_MAXUINT;

	/* only use the timer if profiling; it's expensive */
	if (self->profile) {
		g_timer_start(self->timer);
		g_timer_start(self->timer_child);
	}

	/* no more step data */
	self->any_child_has_step_weighting = FALSE;
	g_ptr_array_set_size(self->children, 0);
}

/**
 * fu_progress_set_steps:
 * @self: A #FuProgress
 * @step_max: The number of sub-tasks in this progress, can be 0
 *
 * Sets the number of sub-tasks, i.e. how many times the fu_progress_step_done()
 * function will be called in the loop.
 *
 * The progress ID must be set fu_progress_set_id() before this method is used.
 *
 * Since: 1.7.0
 **/
void
fu_progress_set_steps(FuProgress *self, guint step_max)
{
	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(self->id != NULL);

	/* if there is an insane number of steps, scale these */
	if (step_max > FU_PROGRESS_STEPS_MAX) {
		self->step_scaling = step_max / 100;
		step_max = 100;
	}

	/* create fake steps */
	for (guint i = 0; i < step_max; i++)
		fu_progress_add_step(self, self->status, 0, NULL);

	/* adjust global fraction */
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		child->global_fraction = self->global_fraction / step_max;
		if (child->global_fraction < 0.01f)
			g_signal_handlers_disconnect_by_data(child, self);
	}

	/* show that the sub-progress has been created */
	fu_progress_set_percentage(self, 0);
	fu_progress_add_flag(self, FU_PROGRESS_FLAG_NO_PROFILE);

	/* reset child timer */
	g_timer_start(self->timer_child);
}

/**
 * fu_progress_get_global_fraction:
 * @self: A #FuProgress
 *
 * Gets the global percentage.
 *
 * Return value: fraction, where 1.0 is 100%.
 *
 * Since: 2.0.4
 **/
gdouble
fu_progress_get_global_fraction(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), -1.f);
	return self->global_fraction;
}

/**
 * fu_progress_get_steps:
 * @self: A #FuProgress
 *
 * Gets the number of sub-tasks, i.e. how many times the fu_progress_step_done()
 * function will be called in the loop.
 *
 * Return value: number of sub-tasks in this progress
 *
 * Since: 1.7.0
 **/
guint
fu_progress_get_steps(FuProgress *self)
{
	g_return_val_if_fail(FU_IS_PROGRESS(self), G_MAXUINT);
	return self->children->len;
}

static gdouble
fu_progress_discrete_to_percent(guint discrete, guint step_max)
{
	/* check we are in range */
	if (discrete > step_max)
		return 100;
	if (step_max == 0) {
		g_warning("step_max is 0!");
		return 0;
	}
	return ((gdouble)discrete * (100.0f / (gdouble)(step_max)));
}

static gdouble
fu_progress_get_step_percentage(FuProgress *self, guint idx)
{
	guint current = 0;
	guint total = 0;

	/* just use proportional */
	if (!self->any_child_has_step_weighting)
		return -1;

	/* work out percentage */
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		if (i <= idx)
			current += child->step_weighting;
		total += child->step_weighting;
	}
	if (total == 0)
		return -1;
	return ((gdouble)current * 100.f) / (gdouble)total;
}

static void
fu_progress_child_status_changed_cb(FuProgress *child, FwupdStatus status, FuProgress *self)
{
	fu_progress_set_status(self, status);
}

static void
fu_progress_child_percentage_changed_cb(FuProgress *child, guint percentage, FuProgress *self)
{
	gdouble offset;
	gdouble range;
	gdouble extra;
	guint parent_percentage = G_MAXUINT;

	/* propagate up the stack if FuProgress has only one priv */
	if (self->children->len == 1) {
		fu_progress_set_percentage(self, percentage);
		return;
	}

	/* did we call done on a step that did not have a size set? */
	if (self->children->len == 0)
		return;

	/* already at >= 100% */
	if (self->step_now >= self->children->len) {
		g_warning("already at %u/%u step_max", self->step_now, self->children->len);
		return;
	}

	/* if the child finished, set the status back to the last parent status */
	if (percentage == 100) {
		FuProgress *child_tmp = g_ptr_array_index(self->children, self->step_now);
		if (fu_progress_get_status(child_tmp) != FWUPD_STATUS_UNKNOWN)
			fu_progress_set_status(self, fu_progress_get_status(child_tmp));
	}

	/* we don't store zero */
	if (self->step_now == 0) {
		gdouble pc = fu_progress_get_step_percentage(self, 0);
		if (pc > 0)
			parent_percentage = percentage * pc / 100;
	} else {
		gdouble pc1 = fu_progress_get_step_percentage(self, self->step_now - 1);
		gdouble pc2 = fu_progress_get_step_percentage(self, self->step_now);
		/* bi-linearly interpolate */
		if (pc1 >= 0 && pc2 >= 0)
			parent_percentage = (((100 - percentage) * pc1) + (percentage * pc2)) / 100;
	}
	if (parent_percentage != G_MAXUINT) {
		fu_progress_set_percentage(self, parent_percentage);
		return;
	}

	/* get the range between the parent priv and the next parent priv */
	offset = fu_progress_discrete_to_percent(self->step_now, self->children->len);
	range = fu_progress_discrete_to_percent(self->step_now + 1, self->children->len) - offset;
	if (range < 0.01)
		return;

	/* get the extra contributed by the child */
	extra = ((gdouble)percentage / 100.0f) * range;

	/* emit from the parent */
	parent_percentage = (guint)(offset + extra);
	fu_progress_set_percentage(self, parent_percentage);
}

/**
 * fu_progress_add_step:
 * @self: A #FuProgress
 * @status: status value to use for this phase
 * @value: A step weighting variable argument array
 * @name: (nullable): Human readable name to identify the step
 *
 * This sets the step weighting, which you will want to do if one action
 * will take a bigger chunk of time than another.
 *
 * The progress ID must be set fu_progress_set_id() before this method is used.
 *
 * Since: 1.8.2
 **/
void
fu_progress_add_step(FuProgress *self, FwupdStatus status, guint value, const gchar *name)
{
	g_autoptr(FuProgress) child = fu_progress_new(NULL);

	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(self->id != NULL);
	g_return_if_fail(self->children->len < 100 * 1000);

	/* save data */
	fu_progress_set_status(child, status);
	child->step_weighting = value;

	/* compute ahead of time */
	if (child->step_weighting > 0)
		self->any_child_has_step_weighting = TRUE;

	/* adjust global percentage */
	if (value > 0)
		child->global_fraction = self->global_fraction * (gdouble)value / 100.f;

	/* connect signals as required */
	if (fu_progress_get_global_fraction(self) > 0.001f) {
		g_signal_connect(FU_PROGRESS(child),
				 "percentage-changed",
				 G_CALLBACK(fu_progress_child_percentage_changed_cb),
				 self);
	}
	g_signal_connect(FU_PROGRESS(child),
			 "status-changed",
			 G_CALLBACK(fu_progress_child_status_changed_cb),
			 self);
	fu_progress_set_parent(child, self);
	if (name != NULL)
		fu_progress_set_name(child, name);

	/* use first child status */
	if (self->children->len == 0)
		fu_progress_set_status(self, status);

	/* add child */
	g_ptr_array_add(self->children, g_steal_pointer(&child));

	/* reset child timer */
	g_timer_start(self->timer_child);
}

/**
 * fu_progress_finished:
 * @self: A #FuProgress
 *
 * Called when the step_now sub-task wants to finish early and still complete.
 *
 * Since: 1.7.0
 **/
void
fu_progress_finished(FuProgress *self)
{
	g_return_if_fail(FU_IS_PROGRESS(self));

	/* is already at 100%? */
	if (self->step_now == self->children->len)
		return;

	/* all done */
	self->step_now = self->children->len;
	fu_progress_set_percentage(self, 100);

	/* we finished early, so invalidate children */
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		fu_progress_add_flag(child, FU_PROGRESS_FLAG_NO_TRACEBACK);
	}
}

/**
 * fu_progress_get_child:
 * @self: A #FuProgress
 *
 * Monitor a child and proxy back up to the parent with the correct percentage.
 *
 * Return value: (transfer none): A new %FuProgress or %NULL for failure
 *
 * Since: 1.7.0
 **/
FuProgress *
fu_progress_get_child(FuProgress *self)
{
	guint step_now;

	g_return_val_if_fail(FU_IS_PROGRESS(self), NULL);
	g_return_val_if_fail(self->id != NULL, NULL);

	step_now = self->step_now / self->step_scaling;

	g_return_val_if_fail(self->children->len > 0, NULL);
	g_return_val_if_fail(self->children->len > step_now, NULL);

	/* all preallocated, nothing to do */
	return FU_PROGRESS(g_ptr_array_index(self->children, step_now));
}

static void
fu_progress_show_profile(FuProgress *self)
{
	gdouble division;
	gdouble total_time = 0.0f;
	gboolean close_enough = TRUE;
	g_autoptr(GString) str = NULL;

	/* not accurate enough for a profile result */
	if (self->flags & FU_PROGRESS_FLAG_NO_PROFILE)
		return;

	/* get the total time so we can work out the divisor */
	str = g_string_new("raw timing data was { ");
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		g_string_append_printf(str, "%.3f, ", fu_progress_get_duration(child));
	}
	if (self->children->len > 0)
		g_string_set_size(str, str->len - 2);
	g_string_append(str, " } -- ");

	/* get the total time so we can work out the divisor */
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		total_time += fu_progress_get_duration(child);
	}
	if (total_time < 0.001)
		return;
	division = total_time / 100.0f;

	/* what we set */
	g_string_append(str, "steps were set as [ ");
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		g_string_append_printf(str, "%u ", child->step_weighting);
	}

	/* what we _should_ have set */
	g_string_append_printf(str, "] but should have been [ ");
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		g_string_append_printf(str, "%.0f ", fu_progress_get_duration(child) / division);

		/* this is sufficiently different to what we guessed */
		if (fabs((fu_progress_get_duration(child) / division) -
			 (gdouble)child->step_weighting) > 5) {
			close_enough = FALSE;
		}
	}
	g_string_append(str, "]");
	if (self->flags & FU_PROGRESS_FLAG_GUESSED) {
#ifdef SUPPORTED_BUILD
		g_debug("%s at %s [%s]", str->str, self->id, fu_progress_get_name_fallback(self));
#else
		g_warning("%s at %s [%s]", str->str, self->id, fu_progress_get_name_fallback(self));
		g_warning("Please see "
			  "https://github.com/fwupd/fwupd/wiki/Daemon-Warning:-FuProgress-steps");
#endif
	} else if (!close_enough) {
		g_debug("%s at %s", str->str, self->id);
	}
}

/**
 * fu_progress_step_done:
 * @self: A #FuProgress
 *
 * Called when the step_now sub-task has finished.
 *
 * Since: 1.7.0
 **/
void
fu_progress_step_done(FuProgress *self)
{
	FuProgress *child = NULL;
	gdouble percentage;

	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(self->id != NULL);

	/* ignore steps */
	if (self->step_scaling > 1) {
		if (self->step_now >= self->children->len ||
		    self->step_done++ % self->step_scaling != 0)
			return;
	}

	/* did we call done when no size set? */
	if (self->children->len == 0) {
		g_autoptr(GString) str = g_string_new(NULL);
		fu_progress_build_parent_chain(self, str, 0);
		g_warning("progress done when no size set! [%s]: %s", self->id, str->str);
		return;
	}

	/* get the active child */
	if (self->children->len > 0) {
		if (self->step_now >= self->children->len) {
			g_autoptr(GString) str = g_string_new(NULL);
			fu_progress_build_parent_chain(self, str, 0);
			g_warning("progress done when no children left! [%s]: %s",
				  self->id,
				  str->str);
			return;
		}
		child = g_ptr_array_index(self->children, self->step_now);
	}

	/* save the duration in the array */
	if (self->profile) {
		if (child != NULL)
			fu_progress_set_duration(child, g_timer_elapsed(self->timer_child, NULL));
		g_timer_start(self->timer_child);
	}

	/* is already at 100%? */
	if (self->step_now >= self->children->len) {
		g_autoptr(GString) str = g_string_new(NULL);
		fu_progress_build_parent_chain(self, str, 0);
		g_warning("already at 100%% [%s]: %s", self->id, str->str);
		return;
	}

	/* is child not at 100%? */
	if (!fu_progress_has_flag(self, FU_PROGRESS_FLAG_CHILD_FINISHED) && child != NULL) {
		if (child->step_now != child->children->len) {
			g_autoptr(GString) str = g_string_new(NULL);
			fu_progress_build_parent_chain(child, str, 0);
			g_warning("child is at %u/%u step_max and parent done [%s]\n%s",
				  child->step_now,
				  child->children->len,
				  self->id,
				  str->str);
			/* do not abort, as we want to clean this up */
		}
	}

	/* another */
	self->step_now++;

	/* update status */
	if (self->step_now < self->children->len) {
		FuProgress *child_tmp = g_ptr_array_index(self->children, self->step_now);
		if (fu_progress_get_status(child_tmp) != FWUPD_STATUS_UNKNOWN)
			fu_progress_set_status(self, fu_progress_get_status(child_tmp));
	} else if (self->parent != NULL) {
		fu_progress_set_status(self, fu_progress_get_status(self->parent));
	} else {
		fu_progress_set_status(self, FWUPD_STATUS_UNKNOWN);
	}

	/* not interesting anymore */
	if (self->global_fraction < 0.01)
		return;

	/* find new percentage */
	percentage = fu_progress_get_step_percentage(self, self->step_now - 1);
	if (percentage < 0)
		percentage = fu_progress_discrete_to_percent(self->step_now, self->children->len);
	fu_progress_set_percentage(self, (guint)percentage);

	/* show any profiling stats */
	if (self->profile && self->step_now == self->children->len)
		fu_progress_show_profile(self);
}

/**
 * fu_progress_sleep:
 * @self: a #FuProgress
 * @delay_ms: the delay in milliseconds
 *
 * Sleeps, setting the device progress from 0..100% as time continues.
 *
 * NOTE: You should try to avoid calling this function for emulated devices.
 *
 * Since: 1.7.0
 **/
void
fu_progress_sleep(FuProgress *self, guint delay_ms)
{
	gulong delay_us_pc = (delay_ms * 1000) / 100;

	g_return_if_fail(FU_IS_PROGRESS(self));
	g_return_if_fail(delay_ms > 0);

	fu_progress_set_percentage(self, 0);
	for (guint i = 0; i < 100; i++) {
		g_usleep(delay_us_pc);
		fu_progress_set_percentage(self, i + 1);
	}
}

static void
fu_progress_traceback_cb(FuProgress *self,
			 guint idt,
			 guint child_idx,
			 guint threshold_ms,
			 GString *str)
{
	if (self->flags & FU_PROGRESS_FLAG_NO_TRACEBACK)
		return;
	if (self->children->len == 0 && fu_progress_get_duration(self) < 0.0001)
		return;
	if (threshold_ms == 0 || fu_progress_get_duration(self) * 1000 > threshold_ms) {
		for (guint i = 0; i < idt; i++)
			g_string_append(str, " ");
		if (self->id != NULL)
			g_string_append(str, self->id);
		if (self->name != NULL)
			g_string_append_printf(str, ":%s", self->name);
		if (self->id == NULL && self->name == NULL && child_idx != G_MAXUINT)
			g_string_append_printf(str, "@%u", child_idx);
		g_string_append_printf(str, " [%.2fms]", fu_progress_get_duration(self) * 1000.f);
		g_string_append(str, self->children->len > 0 ? ":\n" : "\n");
	}
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		fu_progress_traceback_cb(child, idt + 4, i, threshold_ms, str);
	}
}

/**
 * fu_progress_traceback:
 * @self: A #FuProgress
 *
 * Create a traceback used for profiling startup.
 *
 * Return value: (transfer full): string
 *
 * Since: 1.8.2
 **/
gchar *
fu_progress_traceback(FuProgress *self)
{
	const gchar *tmp = g_getenv("FWUPD_PROFILE");
	guint64 threshold_ms = 5000;
	g_autoptr(GString) str = g_string_new(NULL);

	/* allow override */
	if (tmp != NULL) {
		g_autoptr(GError) error_local = NULL;
		if (!fu_strtoull(tmp,
				 &threshold_ms,
				 0,
				 G_MAXUINT,
				 FU_INTEGER_BASE_AUTO,
				 &error_local))
			g_warning("invalid threshold value: %s", tmp);
	}

	fu_progress_traceback_cb(self, 0, G_MAXUINT, threshold_ms, str);
	if (str->len == 0)
		return NULL;
	return g_string_free(g_steal_pointer(&str), FALSE);
}

static void
fu_progress_add_string(FwupdCodec *codec, guint idt, GString *str)
{
	FuProgress *self = FU_PROGRESS(codec);

	/* not interesting */
	if (self->id == NULL && self->name == NULL)
		return;

	fwupd_codec_string_append(str, idt, "Id", self->id);
	fwupd_codec_string_append(str, idt, "Name", self->name);
	if (self->percentage != G_MAXUINT)
		fwupd_codec_string_append_int(str, idt, "Percentage", self->percentage);
	if (self->status != FWUPD_STATUS_UNKNOWN)
		fwupd_codec_string_append(str, idt, "Status", fwupd_status_to_string(self->status));
	if (self->duration > 0.0001)
		fwupd_codec_string_append_int(str, idt, "DurationMs", self->duration * 1000.f);
	fwupd_codec_string_append_int(str, idt, "StepWeighting", self->step_weighting);
	fwupd_codec_string_append_int(str, idt, "StepNow", self->step_now);
	for (guint i = 0; i < self->children->len; i++) {
		FuProgress *child = g_ptr_array_index(self->children, i);
		fwupd_codec_add_string(FWUPD_CODEC(child), idt + 1, str);
	}
}

static void
fu_progress_codec_iface_init(FwupdCodecInterface *iface)
{
	iface->add_string = fu_progress_add_string;
}

static void
fu_progress_init(FuProgress *self)
{
	self->status = FWUPD_STATUS_UNKNOWN;
	self->step_scaling = 1;
	self->percentage = G_MAXUINT;
	self->timer = g_timer_new();
	self->timer_child = g_timer_new();
	self->children = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
	self->duration = 0.f;
	self->global_fraction = 1.f;
}

static void
fu_progress_finalize(GObject *object)
{
	FuProgress *self = FU_PROGRESS(object);

	/* show any profiling stats */
	if (self->profile)
		fu_progress_show_profile(self);

	fu_progress_reset(self);
	g_free(self->id);
	g_free(self->name);
	g_ptr_array_unref(self->children);
	g_timer_destroy(self->timer);
	g_timer_destroy(self->timer_child);

	G_OBJECT_CLASS(fu_progress_parent_class)->finalize(object);
}

static void
fu_progress_class_init(FuProgressClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	object_class->finalize = fu_progress_finalize;

	/**
	 * FuProgress::percentage-changed:
	 * @self: the #FuProgress instance that emitted the signal
	 * @percentage: the new value
	 *
	 * The ::percentage-changed signal is emitted when the tasks completion has changed.
	 *
	 * Since: 1.7.0
	 **/
	signals[SIGNAL_PERCENTAGE_CHANGED] = g_signal_new("percentage-changed",
							  G_TYPE_FROM_CLASS(object_class),
							  G_SIGNAL_RUN_LAST,
							  0,
							  NULL,
							  NULL,
							  g_cclosure_marshal_VOID__UINT,
							  G_TYPE_NONE,
							  1,
							  G_TYPE_UINT);
	/**
	 * FuProgress::status-changed:
	 * @self: the #FuProgress instance that emitted the signal
	 * @status: the new #FwupdStatus
	 *
	 * The ::status-changed signal is emitted when the task status has changed.
	 *
	 * Since: 1.7.0
	 **/
	signals[SIGNAL_STATUS_CHANGED] = g_signal_new("status-changed",
						      G_TYPE_FROM_CLASS(object_class),
						      G_SIGNAL_RUN_LAST,
						      0,
						      NULL,
						      NULL,
						      g_cclosure_marshal_VOID__UINT,
						      G_TYPE_NONE,
						      1,
						      G_TYPE_UINT);
}

/**
 * fu_progress_new:
 * @id: (nullable): progress ID, normally `G_STRLOC`
 *
 * Return value: A new #FuProgress instance.
 *
 * Since: 1.7.0
 **/
FuProgress *
fu_progress_new(const gchar *id)
{
	FuProgress *self;
	self = g_object_new(FU_TYPE_PROGRESS, NULL);
	if (id != NULL)
		fu_progress_set_id(self, id);
	return FU_PROGRESS(self);
}