The Pedigree Project  0.1
TimeTracker.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/process/TimeTracker.h"
21 #include "pedigree/kernel/process/Process.h"
22 #include "pedigree/kernel/process/Thread.h"
23 #include "pedigree/kernel/processor/Processor.h"
24 #include "pedigree/kernel/processor/ProcessorInformation.h"
25 
26 TimeTracker::TimeTracker(Process *pProcess, bool fromUserspace)
27  : m_pProcess(pProcess), m_bFromUserspace(fromUserspace)
28 {
29  if (m_pProcess == 0)
30  {
31  // We can get called early, so ensure we don't make any
32  // assumptions about what's present.
33  Thread *pThread = Processor::information().getCurrentThread();
34  if (!pThread)
35  return;
36  m_pProcess = pThread->getParent();
37  if (!m_pProcess)
38  return;
39  }
40 
41  // Track time already spent wherever we were previously.
42  m_pProcess->trackTime(m_bFromUserspace);
43 
44  // Record current time for the next tracking.
45  m_pProcess->recordTime(!m_bFromUserspace);
46 }
47 
48 TimeTracker::~TimeTracker()
49 {
50  if (!m_pProcess)
51  return;
52 
53  // Track time spent in the RAII section.
54  m_pProcess->trackTime(!m_bFromUserspace);
55 
56  // Record current time for future tracking.
57  m_pProcess->recordTime(m_bFromUserspace);
58 }
static ProcessorInformation & information()
Definition: Processor.cc:45
Process * getParent() const
Definition: Thread.h:181
Definition: Thread.h:54
void trackTime(bool bUserspace)
Definition: Process.h:336