示例#1
0
文件: nonportable.py 项目: taol/crepy
def get_resources():
  """
  <Purpose>
    Returns the resource utilization limits as well
    as the current resource utilization.

  <Arguments>
    None.

  <Returns>
    A tuple of dictionaries and an array (limits, usage, stoptimes).

    Limits is the dictionary which maps the resource name
    to its maximum limit.

    Usage is the dictionary which maps the resource name
    to its current usage.

    Stoptimes is an array of tuples with the times which the Repy process
    was stopped and for how long, due to CPU over-use.
    Each entry in the array is a tuple (TOS, Sleep Time) where TOS is the
    time of stop (respective to getruntime()) and Sleep Time is how long the
    repy process was suspended.

    The stop times array holds a fixed number of the last stop times.
    Currently, it holds the last 100 stop times.
  """
  # Acquire the lock...
  get_resources_lock.acquire()

  # ...but always release it
  try:
    # Construct the dictionaries as copies from nanny
    (limits,usage) = nanny.get_resource_information()


    # Calculate all the usage's
    pid = os.getpid()

    # Get CPU and memory, this is thread specific
    if ostype in ["Linux", "Darwin"]:
    
      # Get CPU first, then memory
      usage["cpu"] = os_api.get_process_cpu_time(pid)

      # This uses the cached PID data from the CPU check
      usage["memory"] = os_api.get_process_rss()

      # Get the thread specific CPU usage
      usage["threadcpu"] = os_api.get_current_thread_cpu_time() 


    # Windows Specific versions
    elif ostype in ["Windows","WindowsCE"]:
    
      # Get the CPU time
      usage["cpu"] = windows_api.get_process_cpu_time(pid)

      # Get the memory, use the resident set size
      usage["memory"] = windows_api.process_memory_info(pid)['WorkingSetSize'] 

      # Get thread-level CPU 
      usage["threadcpu"] = windows_api.get_current_thread_cpu_time()

    # Unknown OS
    else:
      raise EnvironmentError("Unsupported Platform!")

    # Use the cached disk used amount
    usage["diskused"] = cached_disk_used

  finally:
    # Release the lock
    get_resources_lock.release()

  # Copy the stop times
  stoptimes = process_stopped_timeline[:]

  # Return the dictionaries and the stoptimes
  return (limits,usage,stoptimes)
示例#2
0
def get_resources():
    """
  <Purpose>
    Returns the resource utilization limits as well
    as the current resource utilization.

  <Arguments>
    None.

  <Returns>
    A tuple of dictionaries and an array (limits, usage, stoptimes).

    Limits is the dictionary which maps the resource name
    to its maximum limit.

    Usage is the dictionary which maps the resource name
    to its current usage.

    Stoptimes is an array of tuples with the times which the Repy process
    was stopped and for how long, due to CPU over-use.
    Each entry in the array is a tuple (TOS, Sleep Time) where TOS is the
    time of stop (respective to getruntime()) and Sleep Time is how long the
    repy process was suspended.

    The stop times array holds a fixed number of the last stop times.
    Currently, it holds the last 100 stop times.
  """
    # Acquire the lock...
    get_resources_lock.acquire()

    # ...but always release it
    try:
        # Construct the dictionaries as copies from nanny
        (limits, usage) = nanny.get_resource_information()

        # Calculate all the usage's
        pid = os.getpid()

        # Get CPU and memory, this is thread specific
        if ostype in ["Linux", "Darwin"]:

            # Get CPU first, then memory
            usage["cpu"] = os_api.get_process_cpu_time(pid)

            # This uses the cached PID data from the CPU check
            usage["memory"] = os_api.get_process_rss()

            # Get the thread specific CPU usage
            usage["threadcpu"] = os_api.get_current_thread_cpu_time()

        # Windows Specific versions
        elif ostype in ["Windows"]:

            # Get the CPU time
            usage["cpu"] = windows_api.get_process_cpu_time(pid)

            # Get the memory, use the resident set size
            usage["memory"] = windows_api.process_memory_info(
                pid)['WorkingSetSize']

            # Get thread-level CPU
            usage["threadcpu"] = windows_api.get_current_thread_cpu_time()

        # Unknown OS
        else:
            raise EnvironmentError("Unsupported Platform!")

        # Use the cached disk used amount
        usage["diskused"] = cached_disk_used

    finally:
        # Release the lock
        get_resources_lock.release()

    # Copy the stop times
    stoptimes = process_stopped_timeline[:]

    # Return the dictionaries and the stoptimes
    return (limits, usage, stoptimes)