How to write a profiler?

后端 未结 5 881
予麋鹿
予麋鹿 2020-12-28 18:00

i would to know how to write a profiler? What books and / or articles recommended? Can anyone help me please?

Someone has already done something like this?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 18:25

    Encouraging lot, aren't we :)

    Profilers aren't too hard if you're just trying to get a reasonable idea of where the program's spending most of its time. If you're bothered about high accuracy and minimum disruption, things get difficult.

    So if yoyu just want the answers a profiler would give you, go for one someone else has written. If you're looking for the intellectual challenge, why not have a go at writing one?

    I've written a couple, for run time environments that the years have rendered irrelevant.

    There are two approaches

    • adding something to each function or other significant point that logs the time and where it is.

    • having a timer going off regularly and taking a peek where the program currently is.

    The JVMPI version seems to be the first kind - the link provided by uzhin shows that it can report on quite a number of things (see section 1.3). What gets executed changes to do this, so the profiling can affect the performance (and if you're profiling what was otherwise a very lightweight but often called function, it can mislead).

    If you can get a timer/interrupt telling you where the program counter was at the time of the interrupt, you can use the symbol table/debugging information to work out which function it was in at the time. This provides less information but can be less disruptive. A bit more information can be obtained from walking the call stack to identify callers etc. I've no idea if these is even possible in Java...

    Paul.

提交回复
热议问题