% Energy measurement script for JVET-K0106

% With this script, the decoding energy for any bit stream "bitsteam_file"
% can be measured using the decoder executable in "decoder_call". It is
% recommended to perform multiple measurements to obtain statistically
% significant results and to detect outliers. "E_dec" contains the
% following decoding energies: 
% - Column 1: Package (CPU cores plus memory)
% - Column 2: CPU (only cores)
% - Column 3: Uncore (only memory)
% For energy measurements, Intel's RAPL tool is used. 
% - https://01.org/rapl-power-meter
% - D. Hackenberg, R. Schone, T. Ilsche, D. Molka, J. Schuchart, and
%   R. Geyer, An energy efficiency feature survey of the intel haswell processor,
%   in Proc. IEEE International Parallel and Distributed Processing
%   Symposium Workshop (IPDPSW), May 2015, pp. 896904.

% Christian Herglotz, July 2018


bitstream_file = 'test.bin';


decoder_call = './TAppDecoderStatic';


nMeasurements = 5; % To ensure validity
E_idle = zeros(nMeasurements,3);
E_act = zeros(nMeasurements,3);
E_dec = zeros(nMeasurements,3);

for i=1:nMeasurements
    
    
    % Read values with decoding
    [stat package1] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj');
    [stat cpu1] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:0/energy_uj');
    [stat ram1] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:1/energy_uj');
    
    % Perform decoding
    tic;
    system([decoder_call ' -b ' bitstream_file ' -o /dev/null']);    
    timeEnd = toc; 
        
    [stat package2] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj');
    [stat cpu2] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:0/energy_uj');
    [stat ram2] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:1/energy_uj');
    
    decTime = timeEnd;
    
    % Read values without decoding
    [stat package3] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj');
    [stat cpu3] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:0/energy_uj');
    [stat ram3] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:1/energy_uj');
    
    pause(decTime);
    
    [stat package4] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj');
    [stat cpu4] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:0/energy_uj');
    [stat ram4] = system('cat /sys/class/powercap/intel-rapl/intel-rapl:0/intel-rapl:0:1/energy_uj');
    
    
    % Save values
    E_act(i,1) = str2num(package2)-str2num(package1);
    E_act(i,2) = str2num(cpu2) - str2num(cpu1);
    E_act(i,3) = str2num(ram2) - str2num(ram1);
    
    E_idle(i,1) = str2num(package4)-str2num(package3);
    E_idle(i,2) = str2num(cpu4) - str2num(cpu3);
    E_idle(i,3) = str2num(ram4) - str2num(ram3);
    
end

% Decoding energies for package, CPU, and non-CPU
E_dec = E_act-E_idle;

save('energy_values.mat');