# Path: FileHandling.py
# def getCPUTimes(workDir):
#     """ Extract and add up the total CPU times from the job report """
#     # Note: this is used with Event Service jobs
# 
#     # Input:  workDir (location of jobReport.json)
#     # Output: cpuCU (unit), totalCPUTime, conversionFactor
# 
#     totalCPUTime = 0L
# 
#     jobReport_dictionary = getJobReport(workDir)
#     if jobReport_dictionary != {}:
#         if jobReport_dictionary.has_key('resource'):
#             resource_dictionary = jobReport_dictionary['resource']
#             if resource_dictionary.has_key('executor'):
#                 executor_dictionary = resource_dictionary['executor']
#                 for format in executor_dictionary.keys(): # "RAWtoESD", ..
#                     if executor_dictionary[format].has_key('cpuTime'):
#                         try:
#                             totalCPUTime += executor_dictionary[format]['cpuTime']
#                         except:
#                             pass
#                     else:
#                         tolog("Format %s has no such key: cpuTime" % (format))
#             else:
#                 tolog("No such key: executor")
#         else:
#             tolog("No such key: resource")
# 
#     conversionFactor = 1.0
#     cpuCU = "s"
# 
#     return cpuCU, totalCPUTime, conversionFactor


import inspect
import commands
import os
import re
import signal
import sys
import time
import Queue
import multiprocessing
import subprocess
import threading
import json
import traceback
    import yampl
from pandayoda.yodacore import Logger
from FileHandling import getCPUTimes
from signal_block.signal_block import block_sig, unblock_sig
                utime = proctimes.split(' ')[13]
                # get stime from proc/<pid>/stat, 15 item
                stime = proctimes.split(' ')[14]
                # count total process used time
                proctotal = int(utime) + int(stime)
            return(float(proctotal))
        except:
            # self.__log.debug("Rank %s: Failed to get cpu consumption time for pid %s: %s" % (self.__rank, pid, traceback.format_exc()))
            return 0

    def getCPUConsumptionTimeFromProc(self):
        cpuConsumptionTime = 0L
        try:
            CLOCK_TICKS = os.sysconf("SC_CLK_TCK")
            if self.__child_pid:
                self.__childProcs = []
                self.getChildren(self.__child_pid)
                for process in self.__childProcs:
                    if process not in self.__child_cpuTime.keys():
                        self.__child_cpuTime[process] = 0
                for process in self.__child_cpuTime.keys():
                    cpuTime = self.getCPUConsumptionTimeFromProcPid(process) / CLOCK_TICKS
                    if cpuTime > self.__child_cpuTime[process]:
                        self.__child_cpuTime[process] = cpuTime
                    cpuConsumptionTime += self.__child_cpuTime[process]
        except:
            self.__log.debug("Rank %s: Failed to get cpu consumption time from proc: %s" % (self.__rank, traceback.format_exc()))
        return cpuConsumptionTime

    def getCPUConsumptionTimeReal(self):

# Path: FileHandling.py
# def getCPUTimes(workDir):
#     """ Extract and add up the total CPU times from the job report """
#     # Note: this is used with Event Service jobs
# 
#     # Input:  workDir (location of jobReport.json)
#     # Output: cpuCU (unit), totalCPUTime, conversionFactor
# 
#     totalCPUTime = 0L
# 
#     jobReport_dictionary = getJobReport(workDir)
#     if jobReport_dictionary != {}:
#         if jobReport_dictionary.has_key('resource'):
#             resource_dictionary = jobReport_dictionary['resource']
#             if resource_dictionary.has_key('executor'):
#                 executor_dictionary = resource_dictionary['executor']
#                 for format in executor_dictionary.keys(): # "RAWtoESD", ..
#                     if executor_dictionary[format].has_key('cpuTime'):
#                         try:
#                             totalCPUTime += executor_dictionary[format]['cpuTime']
#                         except:
#                             pass
#                     else:
#                         tolog("Format %s has no such key: cpuTime" % (format))
#             else:
#                 tolog("No such key: executor")
#         else:
#             tolog("No such key: resource")
# 
#     conversionFactor = 1.0
#     cpuCU = "s"
# 
#     return cpuCU, totalCPUTime, conversionFactor


# Path: HPC/EventServer/EventServerJobManager.py
import inspect
import commands
import os
import re
import signal
import sys
import time
import Queue
import multiprocessing
import subprocess
import threading
import json
import traceback
    import yampl
from pandayoda.yodacore import Logger
from FileHandling import getCPUTimes
from signal_block.signal_block import block_sig, unblock_sig

                utime = proctimes.split(' ')[13]
                # get stime from proc/<pid>/stat, 15 item
                stime = proctimes.split(' ')[14]
                # count total process used time
                proctotal = int(utime) + int(stime)
            return(float(proctotal))
        except:
            # self.__log.debug("Rank %s: Failed to get cpu consumption time for pid %s: %s" % (self.__rank, pid, traceback.format_exc()))
            return 0

    def getCPUConsumptionTimeFromProc(self):
        cpuConsumptionTime = 0L
        try:
            CLOCK_TICKS = os.sysconf("SC_CLK_TCK")
            if self.__child_pid:
                self.__childProcs = []
                self.getChildren(self.__child_pid)
                for process in self.__childProcs:
                    if process not in self.__child_cpuTime.keys():
                        self.__child_cpuTime[process] = 0
                for process in self.__child_cpuTime.keys():
                    cpuTime = self.getCPUConsumptionTimeFromProcPid(process) / CLOCK_TICKS
                    if cpuTime > self.__child_cpuTime[process]:
                        self.__child_cpuTime[process] = cpuTime
                    cpuConsumptionTime += self.__child_cpuTime[process]
        except:
            self.__log.debug("Rank %s: Failed to get cpu consumption time from proc: %s" % (self.__rank, traceback.format_exc()))
        return cpuConsumptionTime

    def getCPUConsumptionTimeReal(self):

        cpuConsumptionUnit, cpuConsumptionTime, cpuConversionFactor = getCPUTimes(os.getcwd())