summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozpower/mozpower/intel_power_gadget.py
blob: 0382084d8f612f49997a1fed0db50ee889abe33c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import csv
import os
import re
import subprocess
import threading
import time

from .mozpowerutils import get_logger


class IPGTimeoutError(Exception):
    """IPGTimeoutError is raised when we cannot stop Intel Power
    Gadget from running. One possble cause of this is not calling
    `stop_ipg` through a `finalize_power_measurements` call. The
    other possiblity is that IPG failed to stop.
    """

    pass


class IPGMissingOutputFileError(Exception):
    """IPGMissingOutputFile is raised when a file path is given
    to _clean_ipg_file but it does not exist or cannot be found
    at the expected location.
    """

    pass


class IPGEmptyFileError(Exception):
    """IPGEmptyFileError is raised when a file path is given
    to _clean_ipg_file and it exists but it is empty (contains
    no results to clean).
    """

    pass


class IPGUnknownValueTypeError(Exception):
    """IPGUnknownValueTypeError is raised when a value within a
    given results file (that was cleaned) cannot be converted to
    its column's expected data type.
    """

    pass


class IntelPowerGadget(object):
    """IntelPowerGadget provides methods for using Intel Power Gadget
    to measure power consumption.

    ::

       from mozpower.intel_power_gadget import IntelPowerGadget

       # On Mac, the ipg_exe_path should point to '/Applications/Intel Power Gadget/PowerLog'
       ipg = IntelPowerGadget(ipg_exe_path, ipg_measure_duration=600, sampling_rate=500)

       ipg.start_ipg()

       # Run tests...

       ipg.stop_ipg()

       # Now process results with IPGResultsHandler by passing it
       # ipg.output_files, ipg.output_file_ext, ipg.ipg_measure_duration,
       # and ipg.sampling_rate.
    """

    def __init__(
        self,
        exe_file_path,
        ipg_measure_duration=10,
        sampling_rate=1000,
        output_file_ext=".txt",
        file_counter=1,
        output_file_path="powerlog",
        logger_name="mozpower",
    ):
        """Initializes the IntelPowerGadget object.

        :param str exe_file_path: path to Intel Power Gadget 'PowerLog' executable.
        :param int ipg_measure_duration: duration to run IPG for in seconds.
            This does not dictate when the tools shuts down. It only stops when stop_ipg
            is called in case the test runs for a non-deterministic amount of time. The
            IPG executable requires a duration to be supplied. The machinery in place is
            to ensure that IPG keeps recording as long as the experiment runs, so
            multiple files may result, which is handled in
            IPGResultsHandler._combine_cumulative_rows. Defaults to 10s.
        :param int sampling_rate: sampling rate of measurements in milliseconds.
            Defaults to 1000ms.
        :param output_file_ext: file extension of data being output. Defaults to '.txt'.
        :param int file_counter: dictates the start of the file numbering (used
            when test time exceeds duration value). Defaults to 0.
        :param str output_file_path: path to the output location combined
            with the output file prefix. Defaults to current working directory using the
            prefix 'powerlog'.
        :param str logger_name: logging logger name. Defaults to 'mozpower'.
        """
        self._logger = get_logger(logger_name)

        # Output-specific settings
        self._file_counter = file_counter
        self._output_files = []
        self._output_file_path = output_file_path
        self._output_file_ext = output_file_ext
        self._output_dir_path, self._output_file_prefix = os.path.split(
            self._output_file_path
        )

        # IPG-specific settings
        self._ipg_measure_duration = ipg_measure_duration  # in seconds
        self._sampling_rate = sampling_rate  # in milliseconds
        self._exe_file_path = exe_file_path

        # Setup thread for measurement gathering
        self._thread = threading.Thread(
            target=self.run, args=(exe_file_path, ipg_measure_duration)
        )
        self._thread.daemon = True
        self._running = False

    @property
    def output_files(self):
        """Returns the list of files produced from running IPG.

        :returns: list
        """
        return self._output_files

    @property
    def output_file_ext(self):
        """Returns the extension of the files produced by IPG.

        :returns: str
        """
        return self._output_file_ext

    @property
    def output_file_prefix(self):
        """Returns the prefix of the files produces by IPG.

        :returns: str
        """
        return self._output_file_prefix

    @property
    def output_dir_path(self):
        """Returns the output directory of the files produced by IPG.

        :returns: str
        """
        return self._output_dir_path

    @property
    def sampling_rate(self):
        """Returns the specified sampling rate.

        :returns: int
        """
        return self._sampling_rate

    @property
    def ipg_measure_duration(self):
        """Returns the IPG measurement duration (see __init__ for description
        of what this value does).

        :returns: int
        """
        return self._ipg_measure_duration

    def start_ipg(self):
        """Starts the thread which runs IPG to start gathering measurements."""
        self._logger.info("Starting IPG thread")
        self._stop = False
        self._thread.start()

    def stop_ipg(self, wait_interval=10, timeout=200):
        """Stops the thread which runs IPG and waits for it to finish it's work.

        :param int wait_interval: interval time (in seconds) at which to check if
            IPG is finished.
        :param int timeout: time to wait until _wait_for_ipg hits a time out,
            in seconds.
        """
        self._logger.info("Stopping IPG thread")
        self._stop = True
        self._wait_for_ipg(wait_interval=wait_interval, timeout=timeout)

    def _wait_for_ipg(self, wait_interval=10, timeout=200):
        """Waits for IPG to finish it's final dataset.

        :param int wait_interval: interval time (in seconds) at which to check if
            IPG is finished.
        :param int timeout: time to wait until this method hits a time out,
            in seconds.
        :raises: * IPGTimeoutError
        """
        timeout_stime = time.time()
        while self._running and (time.time() - timeout_stime) < timeout:
            self._logger.info(
                "Waiting %s sec for Intel Power Gadget to stop" % wait_interval
            )
            time.sleep(wait_interval)
        if self._running:
            raise IPGTimeoutError("Timed out waiting for IPG to stop")

    def _get_output_file_path(self):
        """Returns the next output file name to be used. Starts at 1 and increases
        at each successive call. Used when the test duration exceeds the specified
        duration through ipg_meaure_duration.

        :returns: str
        """
        self._output_file_path = os.path.join(
            self._output_dir_path,
            "%s_%s_%s"
            % (self._output_file_prefix, self._file_counter, self._output_file_ext),
        )
        self._file_counter += 1
        self._logger.info(
            "Creating new file for IPG data measurements: %s" % self._output_file_path
        )
        return self._output_file_path

    def run(self, exe_file_path, ipg_measure_duration):
        """Runs the IPG measurement gatherer. While stop has not been set to True,
        it continuously gathers measurements into separate files that are merged
        by IPGResultsHandler once the output_files are passed to it.

        :param str exe_file_path: file path of where to find the IPG executable.
        :param int ipg_measure_duration: time to gather measurements for.
        """
        self._logger.info("Starting to gather IPG measurements in thread")
        self._running = True

        while not self._stop:
            outname = self._get_output_file_path()
            self._output_files.append(outname)

            try:
                subprocess.check_output(
                    [
                        exe_file_path,
                        "-duration",
                        str(ipg_measure_duration),
                        "-resolution",
                        str(self._sampling_rate),
                        "-file",
                        outname,
                    ],
                    stderr=subprocess.STDOUT,
                )
            except subprocess.CalledProcessError as e:
                error_log = str(e)
                if e.output:
                    error_log = e.output.decode()
                self._logger.critical(
                    "Error while running Intel Power Gadget: %s" % error_log
                )

        self._running = False


class IPGResultsHandler(object):
    """IPGResultsHandler provides methods for cleaning and formatting
    the files that were created by IntelPowerGadget.

    ::

       from mozpower.intel_power_gadget import IntelPowerGadget, IPGResultsHandler

       ipg = IntelPowerGadget(ipg_exe_path, ipg_measure_duration=600, sampling_rate=500)

       # Run tests - call start_ipg and stop_ipg...

       ipg_rh = IPGResultsHandler(
            ipg.output_files,
            ipg.output_dir_path,
            ipg_measure_duration=ipg.ipg_measure_duration,
            sampling_rate=ipg.sampling_rate
        )

       cleaned_data = ipg_rh.clean_ipg_data()
       # you can also get the data from results after calling clean_ipg_data
       cleaned_data = ipg_rh.results

       perfherder_data = ipg_rh.format_ipg_data_to_partial_perfherder(
            experiment_duration,
            test_name
       )
       # you can also get the perfherder data from summarized_results
       # after calling format_ipg_data_to_partial_perfherder
       perfherder_data = ipg_rh.summarized_results
    """

    def __init__(
        self,
        output_files,
        output_dir_path,
        ipg_measure_duration=10,
        sampling_rate=1000,
        logger_name="mozpower",
    ):
        """Initializes the IPGResultsHandler object.

        :param list output_files: files output by IntelPowerGadget containing
            the IPG data.
        :param str output_dir_path: location to store cleaned files and merged data.
        :param int ipg_measure_duration: length of time that each IPG measurement lasted
            in seconds (see IntelPowerGadget for more information on this argument).
            Defaults to 10s.
        :param int sampling_rate: sampling rate of the measurements in milliseconds.
            Defaults to 1000ms.
        :param str logger_name: logging logger name. Defaults to 'mozpower'.
        """
        self._logger = get_logger(logger_name)
        self._results = {}
        self._summarized_results = {}
        self._cleaned_files = []
        self._csv_header = None
        self._merged_output_path = None
        self._output_file_prefix = None
        self._output_file_ext = None

        self._sampling_rate = sampling_rate
        self._ipg_measure_duration = ipg_measure_duration
        self._output_files = output_files
        self._output_dir_path = output_dir_path

        if self._output_files:
            # Gather the output file extension, and prefix
            # for the cleaned files, and the merged file.
            single_file = self._output_files[0]
            _, file = os.path.split(single_file)
            self._output_file_ext = "." + file.split(".")[-1]

            # This prefix detection depends on the path names created
            # by _get_output_file_path.
            integer_re = re.compile(r"""(.*)_[\d+]_%s""" % self._output_file_ext)
            match = integer_re.match(file)
            if match:
                self._output_file_prefix = match.group(1)
            else:
                self._output_file_prefix = file.split("_")[0]
                self._logger.warning(
                    "Cannot find output file prefix from output file name %s"
                    "using the following prefix: %s" % (file, self._output_file_prefix)
                )

    @property
    def results(self):
        """Returns the cleaned IPG data in the form of a dict.
        Each key is a measurement name with the values being the list
        of measurements. All value lists are sorted in increasing time.

        :returns: dict
        """
        return self._results

    @property
    def summarized_results(self):
        """Returns IPG data in the form of a dict that is formatted
        into a perfherder data blob.

        :returns: dict
        """
        return self._summarized_results

    @property
    def cleaned_files(self):
        """Returns a list of cleaned IPG data files that were output
        after running clean_ipg_data.

        :returns: list
        """
        return self._cleaned_files

    @property
    def merged_output_path(self):
        """Returns the path to the cleaned, and merged output file.

        :returns: str
        """
        return self._merged_output_path

    def _clean_ipg_file(self, file):
        """Cleans an individual IPG file and writes out a cleaned file.

        An uncleaned file looks like this (this contains a partial
        sample of the time series data):
        ```
        "System Time","RDTSC","Elapsed Time (sec)"
        "12:11:05:769","61075218263548","   2.002"
        "12:11:06:774","61077822279584","   3.007"
        "12:11:07:778","61080424421708","   4.011"
        "12:11:08:781","61083023535972","   5.013"
        "12:11:09:784","61085623402302","   6.016"

        "Total Elapsed Time (sec) = 10.029232"
        "Measured RDTSC Frequency (GHz) = 2.592"

        "Cumulative Processor Energy_0 (Joules) = 142.337524"
        "Cumulative Processor Energy_0 (mWh) = 39.538201"
        "Average Processor Power_0 (Watt) = 14.192265"

        "Cumulative IA Energy_0 (Joules) = 121.888000"
        "Cumulative IA Energy_0 (mWh) = 33.857778"
        "Average IA Power_0 (Watt) = 12.153273"

        "Cumulative DRAM Energy_0 (Joules) = 7.453308"
        "Cumulative DRAM Energy_0 (mWh) = 2.070363"
        "Average DRAM Power_0 (Watt) = 0.743158"

        "Cumulative GT Energy_0 (Joules) = 0.079834"
        "Cumulative GT Energy_0 (mWh) = 0.022176"
        "Average GT Power_0 (Watt) = 0.007960"
        ```

        While a cleaned file looks like:
        ```
        System Time,RDTSC,Elapsed Time (sec)
        12:11:05:769,61075218263548,   2.002
        12:11:06:774,61077822279584,   3.007
        12:11:07:778,61080424421708,   4.011
        12:11:08:781,61083023535972,   5.013
        12:11:09:784,61085623402302,   6.016
        ```

        The first portion of the file before the '"Total Elapsed Time' entry is
        considered as the time series which is captured as a dict in the returned
        results.

        All the text starting from '"Total Elapsed Time' is removed from the file and
        is considered as a summary of the experiment (which is returned). This
        portion is ignored in any other processing done by IPGResultsHandler,
        and is not saved, unlike the returned results value.

        Note that the new lines are removed from the summary that is returned
        (as well as the results).

        :param str file: file to clean.
        :returns: a tuple of (dict, list, str) for
            (results, summary, clean_output_path)
            see method comments above for information on what
            this output contains.
        :raises: * IPGMissingOutputFileError
                 * IPGEmptyFileError
        """
        self._logger.info("Cleaning IPG data file %s" % file)

        txt = ""
        if os.path.exists(file):
            with open(file, "r") as f:
                txt = f.read()
        else:
            # This should never happen, so prevent IPGResultsHandler
            # from continuing to clean files if it does occur.
            raise IPGMissingOutputFileError(
                "The following file does not exist so it cannot be cleaned: %s " % file
            )

        if txt == "":
            raise IPGEmptyFileError(
                "The following file is empty so it cannot be cleaned: %s" % file
            )

        # Split the time series data from the summary
        tseries, summary = re.split('"Total Elapsed Time', txt)

        # Clean the summary
        summary = '"Total Elapsed Time' + summary
        summary = [
            line for line in re.split(r"""\n""", summary.replace("\r", "")) if line
        ]

        # Clean the time series data, store the clean rows to write out later,
        # and format the rows into a dict entry for each measure.
        results = {}
        clean_rows = []
        csv_header = None
        for c, row in enumerate(
            csv.reader(tseries.split("\n"), quotechar=str('"'), delimiter=str(","))
        ):
            if not row:
                continue

            # Make sure we don't have any bad line endings
            # contaminating the cleaned rows.
            fmt_row = [
                val.replace("\n", "")
                .replace("\t", "")
                .replace("\r", "")
                .replace("\\n", "")
                .strip()
                for val in row
            ]

            if not fmt_row or not any(fmt_row):
                continue

            clean_rows.append(fmt_row)
            if c == 0:
                csv_header = fmt_row
                for col in fmt_row:
                    results[col] = []
                continue
            for i, col in enumerate(fmt_row):
                results[csv_header[i]].append(col)

        # Write out the cleaned data and check to make sure
        # the csv header hasn't changed mid-experiment
        _, fname = os.path.split(file)
        clean_output_path = os.path.join(
            self._output_dir_path,
            fname.replace(
                self._output_file_ext,
                "_clean.%s" % self._output_file_ext.replace(".", ""),
            ),
        )
        self._logger.info("Writing cleaned IPG results to %s" % clean_output_path)
        try:
            with open(clean_output_path, "w") as csvfile:
                writer = csv.writer(csvfile)
                for count, row in enumerate(clean_rows):
                    if count == 0:
                        if self._csv_header is None:
                            self._csv_header = row
                        elif self._csv_header != row:
                            self._logger.warning(
                                "CSV Headers from IPG data have changed during the experiment "
                                "expected: %s; got: %s"
                                % (str(self._csv_header), str(row))
                            )
                    writer.writerow(row)
        except Exception as e:
            self._logger.warning(
                "Could not write out cleaned results of %s to %s due to the following error"
                ", skipping this step: %s" % (file, clean_output_path, str(e))
            )

        # Check to make sure the expected number of samples
        # exist in the file and that the columns have the correct
        # data type.
        column_datatypes = {"System Time": str, "RDTSC": int, "default": float}
        # pylint --py3k W1619
        expected_samples = int(
            self._ipg_measure_duration / (float(self._sampling_rate) / 1000)
        )
        for key in results:
            if len(results[key]) != expected_samples:
                self._logger.warning(
                    "Unexpected number of samples in %s for column %s - "
                    "expected: %s, got: %s"
                    % (clean_output_path, key, expected_samples, len(results[key]))
                )

            dtype = column_datatypes["default"]
            if key in column_datatypes:
                dtype = column_datatypes[key]

            for val in results[key]:
                try:
                    # Check if converting from a string to its expected data
                    # type works, if not, it's not the expected data type.
                    dtype(val)
                except ValueError as e:
                    raise IPGUnknownValueTypeError(
                        "Cleaned file %s entry %s in column %s has unknown type "
                        "instead of the expected type %s - data corrupted, "
                        "cannot continue: %s"
                        % (clean_output_path, str(val), key, dtype.__name__, str(e))
                    )

        # Check to make sure that IPG measured for the expected
        # amount of time.
        etime = "Elapsed Time (sec)"
        if etime in results:
            total_time = int(float(results[etime][-1]))
            if total_time != self._ipg_measure_duration:
                self._logger.warning(
                    "Elapsed time found in file %s is different from expected length - "
                    "expected: %s, got: %s"
                    % (clean_output_path, self._ipg_measure_duration, total_time)
                )
        else:
            self._logger.warning(
                "Missing 'Elapsed Time (sec)' in file %s" % clean_output_path
            )

        return results, summary, clean_output_path

    def _combine_cumulative_rows(self, cumulatives):
        """Combine cumulative rows from multiple IPG files into
        a single time series.

        :param list cumulatives: list of cumulative time series collected
            over time, on for each file. Must be ordered in increasing time.
        :returns: list
        """
        combined_cumulatives = []

        val_mod = 0
        for count, cumulative in enumerate(cumulatives):
            # Add the previous file's maximum cumulative value
            # to the current one
            mod_cumulative = [val_mod + float(val) for val in cumulative]
            val_mod = mod_cumulative[-1]
            combined_cumulatives.extend(mod_cumulative)

        return combined_cumulatives

    def clean_ipg_data(self):
        """Cleans all IPG files, or data, that was produced by an IntelPowerGadget object.
        Returns a dict containing the merged data from all files.

        :returns: dict
        """
        self._logger.info("Cleaning IPG data...")

        if not self._output_files:
            self._logger.warning("No IPG files to clean.")
            return
        # If this is called a second time for the same set of files,
        # then prevent it from duplicating the cleaned file entries.
        if self._cleaned_files:
            self._cleaned_files = []

        # Clean each file individually and gather the results
        all_results = []
        for file in self._output_files:
            results, summary, output_file_path = self._clean_ipg_file(file)
            self._cleaned_files.append(output_file_path)
            all_results.append(results)

        # Merge all the results into a single result
        combined_results = {}
        for measure in all_results[0]:
            lmeasure = measure.lower()
            if "cumulative" not in lmeasure and "elapsed time" not in lmeasure:
                # For measures which are not cumulative, or elapsed time,
                # combine them without changing the data.
                for count, result in enumerate(all_results):
                    if "system time" in lmeasure or "rdtsc" in lmeasure:
                        new_results = result[measure]
                    else:
                        new_results = [float(val) for val in result[measure]]

                    if count == 0:
                        combined_results[measure] = new_results
                    else:
                        combined_results[measure].extend(new_results)
            else:
                # For cumulative, and elapsed time measures, we need to
                # modify all values - see _combine_cumulative_rows for
                # more information on this procedure.
                cumulatives = [result[measure] for result in all_results]
                self._logger.info(
                    "Combining cumulative rows for '%s' measure" % measure
                )
                combined_results[measure] = self._combine_cumulative_rows(cumulatives)

        # Write merged results to a new file
        merged_output_path = os.path.join(
            self._output_dir_path,
            "%s_merged.%s"
            % (self._output_file_prefix, self._output_file_ext.replace(".", "")),
        )

        self._merged_output_path = merged_output_path
        self._logger.info("Writing merged IPG results to %s" % merged_output_path)
        try:
            with open(merged_output_path, "w") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(self._csv_header)

                # Use _csv_header list to keep the header ordering
                # the same as the cleaned and raw files.
                first_key = list(combined_results.keys())[0]
                for row_count, _ in enumerate(combined_results[first_key]):
                    row = []
                    for measure in self._csv_header:
                        row.append(combined_results[measure][row_count])
                    writer.writerow(row)
        except Exception as e:
            self.merged_output_path = None
            self._logger.warning(
                "Could not write out merged results to %s due to the following error"
                ", skipping this step: %s" % (merged_output_path, str(e))
            )

        # Check that the combined results have the expected number of samples.
        # pylint W16919
        expected_samples = int(
            self._ipg_measure_duration / (float(self._sampling_rate) / 1000)
        )
        combined_expected_samples = len(self._cleaned_files) * expected_samples
        for key in combined_results:
            if len(combined_results[key]) != combined_expected_samples:
                self._logger.warning(
                    "Unexpected number of merged samples in %s for column %s - "
                    "expected: %s, got: %s"
                    % (
                        merged_output_path,
                        key,
                        combined_expected_samples,
                        len(results[key]),
                    )
                )

        self._results = combined_results
        return self._results

    def format_ipg_data_to_partial_perfherder(self, duration, test_name):
        """Format the merged IPG data produced by clean_ipg_data into a
        partial/incomplete perfherder data blob. Returns the perfherder
        data and stores it in _summarized_results.

        This perfherder data still needs more information added to it before it
        can be validated against the perfherder schema. The entries returned
        through here are missing many required fields. Furthermore, the 'values'
        entries need to be formatted into 'subtests'.

        Here is a sample of a complete perfherder data which uses
        the 'utilization' results within the subtests:
        ```
        {
            "framework": {"name": "raptor"},
            "type": "power",
            "unit": "mWh"
            "suites": [
                {
                    "name": "raptor-tp6-amazon-firefox-power",
                    "lowerIsBetter": true,
                    "alertThreshold": 2.0,
                    "subtests": [
                        {
                            "lowerIsBetter": true,
                            "unit": "%",
                            "name": "raptor-tp6-youtube-firefox-power-cpu",
                            "value": 14.409090909090908,
                            "alertThreshold": 2.0
                        },
                        {
                            "lowerIsBetter": true,
                            "unit": "%",
                            "name": "raptor-tp6-youtube-firefox-power-gpu",
                            "value": 20.1,
                            "alertThreshold": 2.0
                        },
                    ]
                }
            ]
        }
        ```

        To obtain data that is formatted to a complete perfherder data blob,
        see get_complete_perfherder_data in MozPower.

        :param float duration: the actual duration of the test in case some
            data needs to be cut off from the end.
        :param str test_name: the name of the test.
        :returns: dict
        """
        self._logger.info("Formatting cleaned IPG data into partial perfherder data")

        if not self._results:
            self._logger.warning(
                "No merged results found - cannot format data to perfherder format."
            )
            return

        def replace_measure_name(name):
            """Replaces the long IPG names with shorter versions.
            Returns the given name if no conversions exist.

            :param str name: name of the entry to replace.
            :returns: str
            """
            lname = name.lower()
            if "ia " in lname:
                return "processor-cores"
            elif "processor " in lname:
                return "processor-package"
            elif "gt " in lname:
                return "gpu"
            elif "dram " in lname:
                return "dram"
            else:
                return name

        # Cut out entries which surpass the test duration.
        # Occurs when IPG continues past the call to stop it.
        cut_results = self._results
        if duration:
            cutoff_index = 0
            for count, etime in enumerate(self._results["Elapsed Time (sec)"]):
                if etime > duration:
                    cutoff_index = count
                    break
            if cutoff_index > 0:
                for measure in self._results:
                    cut_results[measure] = self._results[measure][:cutoff_index]

        # Get the cumulative power used in mWh
        cumulative_mwh = {}
        for measure in cut_results:
            if "cumulative" in measure.lower() and "mwh" in measure.lower():
                cumulative_mwh[replace_measure_name(measure)] = float(
                    cut_results[measure][-1]
                )

        # Get the power usage rate in Watts
        watt_usage = {}
        for measure in cut_results:
            if "watt" in measure.lower() and "limit" not in measure.lower():
                # pylint --py3k W1619
                watt_usage[replace_measure_name(measure) + "-avg"] = sum(
                    [float(val) for val in cut_results[measure]]
                ) / len(cut_results[measure])
                watt_usage[replace_measure_name(measure) + "-max"] = max(
                    [float(val) for val in cut_results[measure]]
                )

        # Get average CPU and GPU utilization
        average_utilization = {}
        for utilization in ("CPU Utilization(%)", "GT Utilization(%)"):
            if utilization not in cut_results:
                self._logger.warning(
                    "Could not find measurements for: %s" % utilization
                )
                continue

            utilized_name = utilization.lower()
            if "cpu " in utilized_name:
                utilized_name = "cpu"
            elif "gt " in utilized_name:
                utilized_name = "gpu"

            # pylint --py3k W1619
            average_utilization[utilized_name] = sum(
                [float(val) for val in cut_results[utilization]]
            ) / len(cut_results[utilization])

        # Get average and maximum CPU and GPU frequency
        frequency_info = {"cpu": {}, "gpu": {}}
        for frequency_measure in ("CPU Frequency_0(MHz)", "GT Frequency(MHz)"):
            if frequency_measure not in cut_results:
                self._logger.warning(
                    "Could not find measurements for: %s" % frequency_measure
                )
                continue

            fmeasure_name = frequency_measure.lower()
            if "cpu " in fmeasure_name:
                fmeasure_name = "cpu"
            elif "gt " in fmeasure_name:
                fmeasure_name = "gpu"
            # pylint --py3k W1619

            frequency_info[fmeasure_name]["favg"] = sum(
                [float(val) for val in cut_results[frequency_measure]]
            ) / len(cut_results[frequency_measure])

            frequency_info[fmeasure_name]["fmax"] = max(
                [float(val) for val in cut_results[frequency_measure]]
            )

            frequency_info[fmeasure_name]["fmin"] = min(
                [float(val) for val in cut_results[frequency_measure]]
            )

        summarized_results = {
            "utilization": {
                "type": "power",
                "test": str(test_name) + "-utilization",
                "unit": "%",
                "values": average_utilization,
            },
            "power-usage": {
                "type": "power",
                "test": str(test_name) + "-cumulative",
                "unit": "mWh",
                "values": cumulative_mwh,
            },
            "power-watts": {
                "type": "power",
                "test": str(test_name) + "-watts",
                "unit": "W",
                "values": watt_usage,
            },
            "frequency-cpu": {
                "type": "power",
                "test": str(test_name) + "-frequency-cpu",
                "unit": "MHz",
                "values": frequency_info["cpu"],
            },
            "frequency-gpu": {
                "type": "power",
                "test": str(test_name) + "-frequency-gpu",
                "unit": "MHz",
                "values": frequency_info["gpu"],
            },
        }

        self._summarized_results = summarized_results
        return self._summarized_results