thirdparty/google_appengine/google/appengine/api/quota.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Tue, 10 Nov 2009 18:18:06 +0100
changeset 3085 ded7a67e7e0a
parent 2864 2e0b0af889be
permissions -rwxr-xr-x
Some functions which applies to scoped tags in general moved from TaskTag to Task model. Also, some stylish and whitespace changes and docstrings added.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2309
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2007 Google Inc.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#     http://www.apache.org/licenses/LICENSE-2.0
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
#
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""Access to quota usage for this application."""
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
try:
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
  from google3.apphosting.runtime import _apphosting_runtime___python__apiproxy
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
except ImportError:
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
  _apphosting_runtime___python__apiproxy = None
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
def get_request_cpu_usage():
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
  """Get the amount of CPU used so far for the current request.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
  Returns the number of megacycles used so far for the current
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
  request. Does not include CPU used by API calls.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
  Does nothing when used in the dev_appserver.
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
  """
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
  if _apphosting_runtime___python__apiproxy:
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
    return _apphosting_runtime___python__apiproxy.get_request_cpu_usage()
be1b94099f2d Update Google App Engine to 1.2.2 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
  return 0
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    40
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    41
def get_request_api_cpu_usage():
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    42
  """Get the amount of CPU used so far by API calls during the current request.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    43
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    44
  Returns the number of megacycles used so far by API calls for the current
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    45
  request. Does not include CPU used by code in the request itself.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    46
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    47
  Does nothing when used in the dev_appserver.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    48
  """
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    49
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    50
  if _apphosting_runtime___python__apiproxy:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    51
    return _apphosting_runtime___python__apiproxy.get_request_api_cpu_usage()
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    52
  return 0
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    53
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    54
MCYCLES_PER_SECOND = 1200.0
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    55
"""Megacycles to CPU seconds.  Convert by using a 1.2 GHz 64-bit x86 CPU."""
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    56
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    57
def megacycles_to_cpu_seconds(mcycles):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    58
  """Convert an input value in megacycles to CPU-seconds.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    59
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    60
  Returns a double representing the CPU-seconds the input megacycle value
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    61
  converts to.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    62
  """
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    63
  return mcycles / MCYCLES_PER_SECOND
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    64
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    65
def cpu_seconds_to_megacycles(cpu_secs):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    66
  """Convert an input value in CPU-seconds to megacycles.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    67
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    68
  Returns an integer representing the megacycles the input CPU-seconds value
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    69
  converts to.
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    70
  """
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2309
diff changeset
    71
  return int(cpu_secs * MCYCLES_PER_SECOND)