Skip to content

Instantly share code, notes, and snippets.

@btbytes btbytes/0readme.md forked from lukego/0readme.md
Last active Aug 29, 2015

Embed
What would you like to do?
Performance counter access with inline assembler in Lua
-- rdpmc_all(uint64_t[nfixed+ngeneral] *dst)
--
-- Generate code to read performance monitoring counter values into dst.
--
-- The generated code is specialized based on the number of fixed- and
-- general-purpose performance counters that are available in
-- hardware. These numbers are determined separately via the CPUID instruction.
--
-- In practice on Sandy Bridge - Skylake CPUs the number of
-- fixed-purpose registers is three and the number of general-purpose
-- registers is either four (hyperthreads enabled) or eight
-- (hyperthreads disabled).
function rdpmc_multi (Dst, nfixed, ngeneral)
local offset = 0
-- Read a PMC register value into the next slot of the destination buffer
local function rdpmc (isfixed, index)
local arg = (isfixed and 0x40000000 or 0) + index
| mov ecx, arg
| rdpmc
| mov [edi+offset], eax
| mov [edi+offset+4], edx
offset = offset + 8
end
for i = 0, nfixed-1 do rdpmc(true, i) end
for i = 0, ngeneral-1 do rdpmc(false, i) end
| ret
end
;; Code generated by: rdpmc_multi(Dst, 3, 4)
7fa487095000 B900000040 mov ecx, 0x40000000
7fa487095005 0F33 rdpmc
7fa487095007 8907 mov [rdi], eax
7fa487095009 895704 mov [rdi+0x4], edx
7fa48709500c B901000040 mov ecx, 0x40000001
7fa487095011 0F33 rdpmc
7fa487095013 894708 mov [rdi+0x8], eax
7fa487095016 89570C mov [rdi+0xc], edx
7fa487095019 B902000040 mov ecx, 0x40000002
7fa48709501e 0F33 rdpmc
7fa487095020 894710 mov [rdi+0x10], eax
7fa487095023 895714 mov [rdi+0x14], edx
7fa487095026 B900000000 mov ecx, 0x0
7fa48709502b 0F33 rdpmc
7fa48709502d 894718 mov [rdi+0x18], eax
7fa487095030 89571C mov [rdi+0x1c], edx
7fa487095033 B901000000 mov ecx, 0x1
7fa487095038 0F33 rdpmc
7fa48709503a 894720 mov [rdi+0x20], eax
7fa48709503d 895724 mov [rdi+0x24], edx
7fa487095040 B902000000 mov ecx, 0x2
7fa487095045 0F33 rdpmc
7fa487095047 894728 mov [rdi+0x28], eax
7fa48709504a 89572C mov [rdi+0x2c], edx
7fa48709504d B903000000 mov ecx, 0x3
7fa487095052 0F33 rdpmc
7fa487095054 894730 mov [rdi+0x30], eax
7fa487095057 895734 mov [rdi+0x34], edx
7fa48709505a C3 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.