ducky.profiler module

This module provides support for profiling the virtual machine (machine profilers) and running programs (code profilers). Wrappers for python’s deterministic profilers, and simple statistical profiler of running code are available for usage thoughout the ducky sources.

cProfile is the prefered choice of machine profiling backend, with profile is used as a backup option.

Beware, each thread needs its own profiler instance. These instances can be acquired from ProfilerStore class which handles saving their data when virtual machine exits.

To simplify the usage of profilers in ducky sources in case when user does not need profiling, I chose to provide classes with the same API but these classes does not capture any data. These are called dummy profilers, as opposed to the real ones. Both kinds mimic API of profile.Profile - the real machine profiler is profile.Profile object.

class ducky.profiler.DummyCPUCoreProfiler(core, frequency=17)[source]

Bases: object

Dummy code profiler class. Base class for all code profilers.

Parameters:
  • core (ducky.cpu.CPUCore) – core this profiler captures data from.
  • frequency (int) – sampling frequency, given as an instruction count.
create_stats()[source]

Preprocess collected data before they can be printed, searched or saved.

disable()[source]

Disable collection of profiling data.

dump_stats(filename)[source]

Save collected data into file.

Parameters:filename (string) – path to file.
enable()[source]

Enable collection of profiling data.

take_sample()[source]

Take a sample of current state of CPU core, and store any necessary data.

class ducky.profiler.DummyMachineProfiler(*args, **kwargs)[source]

Bases: object

Dummy machine profiler. Does absolutely nothing.

create_stats()[source]

Preprocess collected data before they can be printed, searched or saved.

disable()[source]

Stop collecting profiling data.

dump_stats(filename)[source]

Save collected data into file.

Parameters:filename (string) – path to file.
enable()[source]

Start collecting profiling data.

class ducky.profiler.ProfileRecord[source]

Bases: object

merge(other)[source]
class ducky.profiler.ProfilerStore[source]

Bases: object

This class manages profiler instances. When in need of a profiler (e.g. in a new thread) get one by calling proper method of ProfilerStore object.

enable_cpu()[source]

Each newly created code profiler will be the real one.

enable_machine()[source]

Each newly created virtual machine profiler will be the real one.

get_core_profiler(core)[source]

Create new code profiler.

Return type:DummyCPUCoreProfiler
get_machine_profiler()[source]

Create and return new machine profiler.

Returns:new machine profiler.
is_cpu_enabled()[source]

Returns True when code profiling is enabled.

Return type:bool
is_machine_enabled()[source]

Returns True when virtual machine profiling is enabled.

Return type:bool
save(logger, directory)[source]

Save all captured data to files. Each created profiler stores its data in separate file.

Parameters:directory (string) – directory where all files are stored.
class ducky.profiler.RealCPUCoreProfiler(core)[source]

Bases: ducky.profiler.DummyCPUCoreProfiler

Real code profiler. This class actually does collect data.

dump_stats(filename)[source]
take_sample()[source]
ducky.profiler.STORE = <ducky.profiler.ProfilerStore object>

Main profiler store