0

I am thinking like in terms of Ethereum and it's gas idea. The gas is how much it costs per unit of work within the Ethereum runtime/vm so to speak. I don't remember where in the source it is but I checked it one time and basically, in the virtual machine they are executing the lowest-level instructions of the platform, and just before or after the instruction is evaluated, a counter is incremented so as to measure the "gas" being used. So each instruction increments a counter.

How would you do this for assembly? What is the ideal approach for it? You could have every instruction followed by a counter increment. Sort of like this:

_start:
    mov eax, 4
    inc COUNTER
    mov ebx, 1
    inc COUNTER
    mov ecx, mensagem
    inc COUNTER
    mov edx, len
    inc COUNTER
    int 0x80
    inc COUNTER

    mov eax, 1
    inc COUNTER
    int 0x8

But that seems a bit hardcoded. Is there instead some special assembly instruction you can call to tell the CPU to count somehow? Or if not, do you need essentially to have a VM interpret your assembly/opcodes, and inject the counter between each instruction call? What is the standard approach here? How do you do this in a performance-optimized way?

In addition, this needs to be done on a per-user basis, so some data is probably going to need to be passed around for the execution context so we know who to charge for these specific instructions. If there is a built in performance assembly instruction, can it be used in this case?

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
Lance Pollard
  • 66,757
  • 77
  • 237
  • 416
  • 1
    There are various performance counters in hardware. See also [man perf-stat](http://man7.org/linux/man-pages/man1/perf-stat.1.html) and various other profiling tools. – Jester May 01 '20 at 01:45
  • Unices have long had the concept of a virtual time alarm, so rather than a wall clock or interval, you would have a timer issue a signal after a certain amount of actual running time. The trick is in preventing the running code from being able to reset the timer, which you can do with some ptrace cleverness – mevets May 01 '20 at 01:50
  • 2
    More commonly, one keeps track of the amount of CPU *time* spent, not the number of instructions (which would be a little unfair, as some instructions can take several orders of magnitude more time than others). Some OSes have mechanisms like process accounting, which tracks the amount of CPU time used by every process and could be used to send a bill to the responsible user. – Nate Eldredge May 01 '20 at 02:36
  • My answer on [How do I determine the number of x86 machine instructions executed in a C program?](https://stackoverflow.com/q/54355631) covers the HW-supported way of using performance counters for the `instructions` event, or binary-instrumentation techniques that can e.g. put in an increment per *basic block* (and take that into account when reading counters later). Also of course there are debugging features like the Trap Flag for HW-supported single stepping [Need an overview of debugging process from the hardware layer](https://stackoverflow.com/q/60025251) – Peter Cordes May 01 '20 at 04:03

0 Answers0