Skip to content

Code is eliminated in @pstats #47

@giordano

Description

@giordano
julia> using LinuxPerf

julia> function picalc(numsteps)
           slice = 1.0/numsteps
           sum = 0.0
           @simd for i = 1:numsteps
               x = (i - 0.5) * slice
               sum = sum + (4.0/(1.0 + x^2))
           end
           return sum * slice
       end
picalc (generic function with 1 method)

julia> @time picalc(10000000000)
  5.002960 seconds
3.1415926535897762

julia> @pstats picalc(10000000000)
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               1.44e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               2.59e+05  100.0%  # 258.8 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> @pstats picalc(10000000000000)
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               1.57e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               3.09e+05  100.0%  # 309.2 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> @pstats nothing
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_frontend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
┌ Warning: LinuxPerf.EventTypeExt(hw:stalled_cycles_backend, false, 0x0000000000000006) not supported, skipping
└ @ LinuxPerf ~/.julia/packages/LinuxPerf/396gD/src/LinuxPerf.jl:301
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
╶ cpu-cycles               2.17e+02  100.0%  #  0.0 cycles per ns
┌ instructions             1.70e+01  100.0%  #  0.1 insns per cycle
│ branch-instructions      5.00e+00  100.0%  # 29.4% of insns
└ branch-misses            2.00e+00  100.0%  # 40.0% of branch insns
┌ task-clock               3.47e+05  100.0%  # 347.5 μs
│ context-switches         0.00e+00  100.0%
│ cpu-migrations           0.00e+00  100.0%
└ page-faults              0.00e+00  100.0%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

julia> versioninfo()
Julia Version 1.11.2
Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 22 × Intel(R) Core(TM) Ultra 7 155H
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 22 virtual cores)

(tmp) pkg> st
Status `/tmp/Project.toml`
  [b4c46c6c] LinuxPerf v0.4.0

picalc(10000000000000) should take 1.4 hours, but it takes ~300 μs inside @pstats, which is about the same time reported for nothing. I see there's some effort to try and avoid the code being eliminated

LinuxPerf.jl/src/LinuxPerf.jl

Lines 1144 to 1145 in eae2705

# trick the compiler not to eliminate the code
stats = rand() < 0 ? val : Stats(bench)

but it doesn't seem to be very effective anymore.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions