1/* Copyright (C) 2021 Free Software Foundation, Inc.
2   Contributed by Oracle.
3
4   This file is part of GNU Binutils.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 51 Franklin Street - Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21#include "config.h"
22#include <assert.h>
23#include "Sample.h"
24#include "util.h"
25#include "Exp_Layout.h"
26
27Sample::Sample (int num)
28{
29  number = num;
30  prusage = NULL;
31  start_time = end_time = 0;
32  start_label = end_label = NULL;
33  validated = false;
34}
35
36Sample::~Sample ()
37{
38  delete prusage;
39  free (start_label);
40  free (end_label);
41}
42
43PrUsage *
44Sample::get_usage ()
45{
46  if (validated == false)
47    {
48      validate_usage ();
49      validated = true;
50    }
51  return prusage;
52}
53
54void
55Sample::validate_usage ()
56{
57  if (prusage == NULL || validated)
58    return;
59  validated = true;
60
61  // Make sure that none of the times are negative, force to zero if so
62  if (prusage->pr_utime < 0)
63    prusage->pr_utime = 0;
64  if (prusage->pr_stime < 0)
65    prusage->pr_stime = 0;
66  if (prusage->pr_ttime < 0)
67    prusage->pr_ttime = 0;
68  if (prusage->pr_tftime < 0)
69    prusage->pr_tftime = 0;
70  if (prusage->pr_dftime < 0)
71    prusage->pr_dftime = 0;
72  if (prusage->pr_kftime < 0)
73    prusage->pr_kftime = 0;
74  if (prusage->pr_ltime < 0)
75    prusage->pr_ltime = 0;
76  if (prusage->pr_slptime < 0)
77    prusage->pr_slptime = 0;
78  if (prusage->pr_wtime < 0)
79    prusage->pr_wtime = 0;
80  if (prusage->pr_stoptime < 0)
81    prusage->pr_stoptime = 0;
82  if (prusage->pr_rtime < 0)
83    prusage->pr_rtime = 0;
84
85  // Now make sure that the sum of states is >= prusage->pr_rtime
86  hrtime_t sum = prusage->pr_utime + prusage->pr_stime + prusage->pr_ttime
87	  + prusage->pr_tftime + prusage->pr_dftime + prusage->pr_kftime
88	  + prusage->pr_ltime + prusage->pr_slptime + prusage->pr_wtime
89	  + prusage->pr_stoptime;
90
91  sum = sum - prusage->pr_rtime;
92  if (sum < 0)// increment sleep time to make it match
93    prusage->pr_slptime = prusage->pr_slptime - sum;
94}
95