Skip to content

Commit c75a3d1

Browse files
Scale perf counters based on times enabled and ran (#209)
* We are certainly using more events than available counters and hence raw counters must be scaled based on time enabled and time running counters. Ensure perf counter running time is > 0 when scaling --------- Signed-off-by: Mahendra Paipuri <[email protected]>
1 parent d8f1b76 commit c75a3d1

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

pkg/collector/perf.go

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ func (c *perfCollector) updateHardwareCounters(
625625
for _, proc := range procs {
626626
pid = proc.PID
627627

628+
var scale float64 = 1.0
629+
628630
if hwProfiler, ok := c.perfHwProfilers[pid]; ok {
629631
hwProfile := &perf.HardwareProfile{}
630632
if err := (*hwProfiler).Profile(hwProfile); err != nil {
@@ -633,32 +635,37 @@ func (c *perfCollector) updateHardwareCounters(
633635
continue
634636
}
635637

638+
// Ensure that TimeRunning is always > 0. If it is zero, counters will be zero as well
639+
if hwProfile.TimeEnabled != nil && hwProfile.TimeRunning != nil && *hwProfile.TimeRunning > 0 {
640+
scale = float64(*hwProfile.TimeEnabled) / float64(*hwProfile.TimeRunning)
641+
}
642+
636643
if hwProfile.CPUCycles != nil {
637-
cgroupHwPerfCounters["cpucycles_total"] += float64(*hwProfile.CPUCycles)
644+
cgroupHwPerfCounters["cpucycles_total"] += scale * float64(*hwProfile.CPUCycles)
638645
}
639646

640647
if hwProfile.Instructions != nil {
641-
cgroupHwPerfCounters["instructions_total"] += float64(*hwProfile.Instructions)
648+
cgroupHwPerfCounters["instructions_total"] += scale * float64(*hwProfile.Instructions)
642649
}
643650

644651
if hwProfile.BranchInstr != nil {
645-
cgroupHwPerfCounters["branch_instructions_total"] += float64(*hwProfile.BranchInstr)
652+
cgroupHwPerfCounters["branch_instructions_total"] += scale * float64(*hwProfile.BranchInstr)
646653
}
647654

648655
if hwProfile.BranchMisses != nil {
649-
cgroupHwPerfCounters["branch_misses_total"] += float64(*hwProfile.BranchMisses)
656+
cgroupHwPerfCounters["branch_misses_total"] += scale * float64(*hwProfile.BranchMisses)
650657
}
651658

652659
if hwProfile.CacheRefs != nil {
653-
cgroupHwPerfCounters["cache_refs_total"] += float64(*hwProfile.CacheRefs)
660+
cgroupHwPerfCounters["cache_refs_total"] += scale * float64(*hwProfile.CacheRefs)
654661
}
655662

656663
if hwProfile.CacheMisses != nil {
657-
cgroupHwPerfCounters["cache_misses_total"] += float64(*hwProfile.CacheMisses)
664+
cgroupHwPerfCounters["cache_misses_total"] += scale * float64(*hwProfile.CacheMisses)
658665
}
659666

660667
if hwProfile.RefCPUCycles != nil {
661-
cgroupHwPerfCounters["ref_cpucycles_total"] += float64(*hwProfile.RefCPUCycles)
668+
cgroupHwPerfCounters["ref_cpucycles_total"] += scale * float64(*hwProfile.RefCPUCycles)
662669
}
663670
}
664671
}
@@ -695,6 +702,8 @@ func (c *perfCollector) updateSoftwareCounters(
695702
for _, proc := range procs {
696703
pid = proc.PID
697704

705+
var scale float64 = 1.0
706+
698707
if swProfiler, ok := c.perfSwProfilers[pid]; ok {
699708
swProfile := &perf.SoftwareProfile{}
700709
if err := (*swProfiler).Profile(swProfile); err != nil {
@@ -703,24 +712,28 @@ func (c *perfCollector) updateSoftwareCounters(
703712
continue
704713
}
705714

715+
if swProfile.TimeEnabled != nil && swProfile.TimeRunning != nil && *swProfile.TimeRunning > 0 {
716+
scale = float64(*swProfile.TimeEnabled) / float64(*swProfile.TimeRunning)
717+
}
718+
706719
if swProfile.PageFaults != nil {
707-
cgroupSwPerfCounters["page_faults_total"] += float64(*swProfile.PageFaults)
720+
cgroupSwPerfCounters["page_faults_total"] += scale * float64(*swProfile.PageFaults)
708721
}
709722

710723
if swProfile.ContextSwitches != nil {
711-
cgroupSwPerfCounters["context_switches_total"] += float64(*swProfile.ContextSwitches)
724+
cgroupSwPerfCounters["context_switches_total"] += scale * float64(*swProfile.ContextSwitches)
712725
}
713726

714727
if swProfile.CPUMigrations != nil {
715-
cgroupSwPerfCounters["cpu_migrations_total"] += float64(*swProfile.CPUMigrations)
728+
cgroupSwPerfCounters["cpu_migrations_total"] += scale * float64(*swProfile.CPUMigrations)
716729
}
717730

718731
if swProfile.MinorPageFaults != nil {
719-
cgroupSwPerfCounters["minor_faults_total"] += float64(*swProfile.MinorPageFaults)
732+
cgroupSwPerfCounters["minor_faults_total"] += scale * float64(*swProfile.MinorPageFaults)
720733
}
721734

722735
if swProfile.MajorPageFaults != nil {
723-
cgroupSwPerfCounters["major_faults_total"] += float64(*swProfile.MajorPageFaults)
736+
cgroupSwPerfCounters["major_faults_total"] += scale * float64(*swProfile.MajorPageFaults)
724737
}
725738
}
726739
}
@@ -753,6 +766,8 @@ func (c *perfCollector) updateCacheCounters(cgroupID string, procs []procfs.Proc
753766
for _, proc := range procs {
754767
pid = proc.PID
755768

769+
var scale float64 = 1.0
770+
756771
if cacheProfiler, ok := c.perfCacheProfilers[pid]; ok {
757772
cacheProfile := &perf.CacheProfile{}
758773
if err := (*cacheProfiler).Profile(cacheProfile); err != nil {
@@ -761,52 +776,56 @@ func (c *perfCollector) updateCacheCounters(cgroupID string, procs []procfs.Proc
761776
continue
762777
}
763778

779+
if cacheProfile.TimeEnabled != nil && cacheProfile.TimeRunning != nil && *cacheProfile.TimeRunning > 0 {
780+
scale = float64(*cacheProfile.TimeEnabled) / float64(*cacheProfile.TimeRunning)
781+
}
782+
764783
if cacheProfile.L1DataReadHit != nil {
765-
cgroupCachePerfCounters["cache_l1d_read_hits_total"] += float64(*cacheProfile.L1DataReadHit)
784+
cgroupCachePerfCounters["cache_l1d_read_hits_total"] += scale * float64(*cacheProfile.L1DataReadHit)
766785
}
767786

768787
if cacheProfile.L1DataReadMiss != nil {
769-
cgroupCachePerfCounters["cache_l1d_read_misses_total"] += float64(*cacheProfile.L1DataReadMiss)
788+
cgroupCachePerfCounters["cache_l1d_read_misses_total"] += scale * float64(*cacheProfile.L1DataReadMiss)
770789
}
771790

772791
if cacheProfile.L1DataWriteHit != nil {
773-
cgroupCachePerfCounters["cache_l1d_write_hits_total"] += float64(*cacheProfile.L1DataWriteHit)
792+
cgroupCachePerfCounters["cache_l1d_write_hits_total"] += scale * float64(*cacheProfile.L1DataWriteHit)
774793
}
775794

776795
if cacheProfile.L1InstrReadMiss != nil {
777-
cgroupCachePerfCounters["cache_l1_instr_read_misses_total"] += float64(*cacheProfile.L1InstrReadMiss)
796+
cgroupCachePerfCounters["cache_l1_instr_read_misses_total"] += scale * float64(*cacheProfile.L1InstrReadMiss)
778797
}
779798

780799
if cacheProfile.InstrTLBReadHit != nil {
781-
cgroupCachePerfCounters["cache_tlb_instr_read_hits_total"] += float64(*cacheProfile.InstrTLBReadHit)
800+
cgroupCachePerfCounters["cache_tlb_instr_read_hits_total"] += scale * float64(*cacheProfile.InstrTLBReadHit)
782801
}
783802

784803
if cacheProfile.InstrTLBReadMiss != nil {
785-
cgroupCachePerfCounters["cache_tlb_instr_read_misses_total"] += float64(*cacheProfile.InstrTLBReadMiss)
804+
cgroupCachePerfCounters["cache_tlb_instr_read_misses_total"] += scale * float64(*cacheProfile.InstrTLBReadMiss)
786805
}
787806

788807
if cacheProfile.LastLevelReadHit != nil {
789-
cgroupCachePerfCounters["cache_ll_read_hits_total"] += float64(*cacheProfile.LastLevelReadHit)
808+
cgroupCachePerfCounters["cache_ll_read_hits_total"] += scale * float64(*cacheProfile.LastLevelReadHit)
790809
}
791810

792811
if cacheProfile.LastLevelReadMiss != nil {
793-
cgroupCachePerfCounters["cache_ll_read_misses_total"] += float64(*cacheProfile.LastLevelReadMiss)
812+
cgroupCachePerfCounters["cache_ll_read_misses_total"] += scale * float64(*cacheProfile.LastLevelReadMiss)
794813
}
795814

796815
if cacheProfile.LastLevelWriteHit != nil {
797-
cgroupCachePerfCounters["cache_ll_write_hits_total"] += float64(*cacheProfile.LastLevelWriteHit)
816+
cgroupCachePerfCounters["cache_ll_write_hits_total"] += scale * float64(*cacheProfile.LastLevelWriteHit)
798817
}
799818

800819
if cacheProfile.LastLevelWriteMiss != nil {
801-
cgroupCachePerfCounters["cache_ll_write_misses_total"] += float64(*cacheProfile.LastLevelWriteMiss)
820+
cgroupCachePerfCounters["cache_ll_write_misses_total"] += scale * float64(*cacheProfile.LastLevelWriteMiss)
802821
}
803822

804823
if cacheProfile.BPUReadHit != nil {
805-
cgroupCachePerfCounters["cache_bpu_read_hits_total"] += float64(*cacheProfile.BPUReadHit)
824+
cgroupCachePerfCounters["cache_bpu_read_hits_total"] += scale * float64(*cacheProfile.BPUReadHit)
806825
}
807826

808827
if cacheProfile.BPUReadMiss != nil {
809-
cgroupCachePerfCounters["cache_bpu_read_misses_total"] += float64(*cacheProfile.BPUReadMiss)
828+
cgroupCachePerfCounters["cache_bpu_read_misses_total"] += scale * float64(*cacheProfile.BPUReadMiss)
810829
}
811830
}
812831
}

0 commit comments

Comments
 (0)