Ejemplo n.º 1
0
def getProcess():
    import win32process, win32api, win32con

    p = {}

    procs = win32process.EnumProcesses()
    for pid in procs:
        try:
            handle = win32api.OpenProcess(
                win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ,
                0, pid)
        except:
            handle = None

        exe = None
        if handle:
            try:
                executablePath = win32process.GetModuleFileNameEx(handle, 0)
                filename = os.path.basename(executablePath)
                ptimes = win32process.GetProcessTimes(handle)
                meminfo = win32process.GetProcessMemoryInfo(handle)
                pagefile = meminfo['PagefileUsage'] / (1024 * 1024)
                workset = meminfo['WorkingSetSize'] / (1024 * 1024)
                p[pid] = (ptimes['UserTime'], ptimes['KernelTime'], pagefile,
                          workset, filename, executablePath)

            except:
                pass

        if handle:
            handle.Close()
    return p
Ejemplo n.º 2
0
def printMem(indent=""):
    if isUnix():
        import commands
        #
        procId = os.getpid()
        res = commands.getoutput('cat /proc/%s/status' % procId).split('\n')
        status = dict()
        for i in res:
            if i != '':
                res2 = i.split(":\t")
                status[res2[0]] = res2[1]
        print indent + "VmSize: %s  VmRSS: %s  VmData: %s " % (
            status['VmSize'], status['VmRSS'], status['VmData'])
    else:
        try:
            import win32process
            import win32con
            import win32api
            procId = win32process.GetCurrentProcessId()
            han = win32api.OpenProcess(
                win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ,
                0, procId)
            procmeminfo = win32process.GetProcessMemoryInfo(han)
            workMem = (procmeminfo["WorkingSetSize"] / 1024.)
            peakWork = (procmeminfo["PeakWorkingSetSize"] / 1024.)
            pageFile = (procmeminfo["PagefileUsage"] / 1024.)
            peakPageFile = (procmeminfo["PeakPagefileUsage"] / 1024.)
            print indent + "WorkMem: %sK  PeakMem: %sK  PageFile: %sK  PeakPageFile: %sK" % (
                workMem, peakWork, pageFile, peakPageFile)
        except:
            print "install pywin32 to be able to compute process memory"
def _task_list():
    psapi = ctypes.windll.psapi
    kernel = ctypes.windll.kernel32

    hModule = ctypes.c_ulong()
    count = ctypes.c_ulong()
    modname = ctypes.c_buffer(30)
    PROCESS_QUERY_INFORMATION = 0x0400
    PROCESS_VM_READ = 0x0010

    pid_list = win32process.EnumProcesses()
    info_list = []

    for pid in pid_list:

        hProcess = kernel.OpenProcess(
            PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid)
        if hProcess:
            psapi.EnumProcessModules(hProcess, ctypes.byref(hModule),
                                     ctypes.sizeof(hModule),
                                     ctypes.byref(count))
            psapi.GetModuleBaseNameA(hProcess, hModule.value, modname,
                                     ctypes.sizeof(modname))
            pname = ctypes.string_at(modname)

            procmeminfo = win32process.GetProcessMemoryInfo(hProcess)
            procmemusage = (procmeminfo["WorkingSetSize"] / 1024)
            info_list.append((pid, pname, procmemusage))

            kernel.CloseHandle(hProcess)

    return info_list
Ejemplo n.º 4
0
    def test_memory_info(self):
        handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION,
                                      win32con.FALSE, self.pid)
        self.addCleanup(win32api.CloseHandle, handle)
        sys_value = win32process.GetProcessMemoryInfo(handle)
        psutil_value = psutil.Process(self.pid).memory_info()
        self.assertEqual(
            sys_value['PeakWorkingSetSize'], psutil_value.peak_wset)
        self.assertEqual(
            sys_value['WorkingSetSize'], psutil_value.wset)
        self.assertEqual(
            sys_value['QuotaPeakPagedPoolUsage'], psutil_value.peak_paged_pool)
        self.assertEqual(
            sys_value['QuotaPagedPoolUsage'], psutil_value.paged_pool)
        self.assertEqual(
            sys_value['QuotaPeakNonPagedPoolUsage'],
            psutil_value.peak_nonpaged_pool)
        self.assertEqual(
            sys_value['QuotaNonPagedPoolUsage'], psutil_value.nonpaged_pool)
        self.assertEqual(
            sys_value['PagefileUsage'], psutil_value.pagefile)
        self.assertEqual(
            sys_value['PeakPagefileUsage'], psutil_value.peak_pagefile)

        self.assertEqual(psutil_value.rss, psutil_value.wset)
        self.assertEqual(psutil_value.vms, psutil_value.pagefile)
Ejemplo n.º 5
0
def on_release(key):
    global currentStageName
    global currentStep
    global stepList
    global stage
    global clickImage
    global delay
    global newStepList
    global startTime
    if hasattr(key, "char"):
        if key.char == '/':

            startTime = datetime.datetime.utcnow()
            if len(currentStageName) == 0:
                stages = {"planet": parseSteps("planet")}
            else:
                stages = {currentStageName: parseSteps(currentStageName)}
            for x in stages:
                stage = stages[x]
            while True:
                if stage.nextStageName:
                    # I know this creates a race condition, I just don't care
                    threading.Thread(target=loadNext,
                                     args=(stages,
                                           stage.nextStageName)).start()
                currentStep = 0
                delay = False
                currentStageName = stage.name
                stepList = stage.steps
                newStepList = []
                for idx, x in enumerate(stepList):
                    replayStep(x)
                    newStepList.append(x)
                print("done: " + currentStageName)
                for i, step in enumerate(newStepList):
                    if step.output == OutputType.MOVE:
                        if newStepList[
                                i +
                                1].output == OutputType.CLICK or newStepList[
                                    i + 1].output == OutputType.LONG_CLICK:
                            stepImage = step.imageName
                            nextStep = newStepList[i + 1]
                            nextStepImage = nextStep.imageName
                            step.imageName = nextStepImage
                            nextStep.imageName = stepImage
                f = open(currentStageName + ".dat", "w")
                stage = formatStage(Stage(currentStageName, newStepList))
                f.write(stage)

                han = win32api.OpenProcess(
                    win32con.PROCESS_QUERY_INFORMATION
                    | win32con.PROCESS_VM_READ, 0, os.getpid())
                process_memory = int(
                    win32process.GetProcessMemoryInfo(han)['WorkingSetSize'])
                print("memory:" + str(process_memory))
                nextStageName = stages[currentStageName].nextStageName
                stage = stages[nextStageName]
                if nextStageName is None:
                    break
Ejemplo n.º 6
0
def xp_memusage():
    """ Retrieve memory usage for own process at Win32 platform. """
    global memsize_available

    assert memsize_available
    current_process = win32process.GetCurrentProcess()
    memory_info = win32process.GetProcessMemoryInfo(current_process)
    return "%d bytes" % memory_info["WorkingSetSize"]
Ejemplo n.º 7
0
 def GetMemoryStats(self, pid):
     try:
         memory_info = win32process.GetProcessMemoryInfo(
             self._GetProcessHandle(pid))
     except pywintypes.error, e:
         errcode = e[0]
         if errcode == 87:  # The process may have been closed.
             return {}
         raise
Ejemplo n.º 8
0
def win32_status():  # pragma: no cover
    info = {}
    hProcess = win32api.GetCurrentProcess()
    pmi = win32process.GetProcessMemoryInfo(hProcess)
    for key, value in pmi.items():
        if key != "PageFaultCount":
            # values in bytes
            value = value / (1024.0**2)
        info[key] = value
    return info
Ejemplo n.º 9
0
    def getProcess(self):

        p = {}

        try:

            systime = self.getSystemTimes()

            p['total'] = systime[1] + systime[2]

            procs = win32process.EnumProcesses()
            for pid in procs:

                if pid ==0: continue

                handle = self.openProcess(pid)

                if handle:
                    try:
                        try:
                            executablePath = win32process.GetModuleFileNameEx(handle, 0)
                        except:
                            executablePath = None
                        
                        if not executablePath:
                            winapp = winappdbg.Process(pid)
                            executablePath = winapp.get_filename()

                        filename = os.path.basename(executablePath)

                        if filename.find('TMatrix') ==0 :
                            continue

                        ptimes = win32process.GetProcessTimes(handle)
                        meminfo = win32process.GetProcessMemoryInfo(handle)
                        pagefile = meminfo['PagefileUsage']/(1024*1024)
                        workset = meminfo['WorkingSetSize']/(1024*1024)
                        p[str(pid)] = (ptimes['UserTime'],ptimes['KernelTime'],pagefile,workset,filename,executablePath)
                        
                    except Exception as myException:
                        self.logger.error(myException)
                    finally:    
                        self.CloseHandle(handle)

        except Exception as  myException:
            self.logger.error(myException)      
        

        return p        
Ejemplo n.º 10
0
 def memoryUsed(self):
     try:
         global osPrcs
         if self.isMSW:
             if osPrcs is None:
                 import win32process as osPrcs
             return osPrcs.GetProcessMemoryInfo(osPrcs.GetCurrentProcess())['WorkingSetSize'] / 1024
         elif sys.platform == "sunos5": # ru_maxrss is broken on sparc
             if osPrcs is None:
                 import resource as osPrcs
             return int(subprocess.getoutput("ps -p {0} -o rss".format(os.getpid())).rpartition('\n')[2])
         else: # unix or linux where ru_maxrss works
             import resource as osPrcs
             return osPrcs.getrusage(osPrcs.RUSAGE_SELF).ru_maxrss # in KB
     except Exception:
         pass
     return 0
Ejemplo n.º 11
0
    def GetStatistics(self):
        retstr = ''
        rights = win32con.PROCESS_ALL_ACCESS
        #totalPeak = 0
        #totalWorking = 0

        handle = None
        try:
            handle = None
            handle = win32api.OpenProcess(rights, 0, self.PID)
            memory = win32process.GetProcessMemoryInfo(handle)

            retstr += '\n' + GetLineDivider()
            retstr += 'Process Name                  : %10s\n' % self.Name
            retstr += 'Process ID                    : %10d\n' % self.PID

            index = 0
            for i in list(memory.keys()):
                if memory[i] < 0:
                    memory[i] = 0
                if index < 2:
                    retstr += '%-30s: %10u\n' % (i, memory[i])
                else:
                    retstr += '%-30s: %10u KB\n' % (i, memory[i] / 1024)
                index = index + 1

            #totalPeak = totalPeak + ( memory["PeakWorkingSetSize"] / 1024 )
            #totalWorking = totalWorking + ( memory["WorkingSetSize"] / 1024 )
        except Exception as inst:
            if len(inst.args) == 3:
                number = inst.args[0]
                function = inst.args[1]
                message = inst.args[2]
                retstr += 'ERROR %#08x (%s): %s\n' % (number, function,
                                                      message)
            else:
                retstr += str(inst) + '\n'
        finally:
            if handle:
                win32api.CloseHandle(handle)

        return retstr + '\n' + GetLineDivider()
Ejemplo n.º 12
0
    def _getData(self, sample):
        while True:  # loop until we have both a "new" and a "last" value for CPU time
            if self._stopping.is_set(): raise Exception('Requested to stop')

            newvalues = {}
            newvalues['time_ns'] = self._timer_ns()
            cputimes = win32process.GetProcessTimes(self._hPid)
            newvalues['cputime_ns'] = (
                cputimes['KernelTime'] + cputimes['UserTime']
            ) * 100  # convert to ns; comes in 100*ns units

            if self._lastValues is not None:
                if newvalues['time_ns'] - self._lastValues['time_ns'] <= 0:
                    # wait a bit longer to avoid div by zero error if the sleeping is somehow messed up
                    self._stopping.wait(min(self.interval, 1))
                    continue

                lastvalues = self._lastValues
                break

            # this is just for the first time _getData is called; need to repeat this once so we have stats to compare to
            self._lastValues = lastvalues = newvalues
            self._stopping.wait(min(self.interval, 1))

        memInfo = win32process.GetProcessMemoryInfo(self._hPid)

        data = {}

        # multiply by 100 to get utilization as a %
        data[ProcessMonitorKey.CPU_CORE_UTILIZATION] = (
            100 * (newvalues['cputime_ns'] - lastvalues['cputime_ns'])) // (
                newvalues['time_ns'] - lastvalues['time_ns'])

        self._lastValues = newvalues

        data[ProcessMonitorKey.
             MEMORY_RESIDENT_KB] = memInfo['WorkingSetSize'] // 1024
        data[ProcessMonitorKey.
             MEMORY_VIRTUAL_KB] = memInfo['PagefileUsage'] // 1024

        return data
Ejemplo n.º 13
0
    def getProcess2(self):
        import win32process, win32api, win32con, os

        p = {}
        p['total'] = 0
        total = 0

        procs = win32process.EnumProcesses()
        for pid in procs:
            try:
                handle = win32api.OpenProcess(
                    win32con.PROCESS_QUERY_INFORMATION
                    | win32con.PROCESS_VM_READ, 0, pid)
            except Exception as myException:
                self.logger.error(myException)
                handle = None

            if handle:
                try:
                    executablePath = win32process.GetModuleFileNameEx(
                        handle, 0)
                    filename = os.path.basename(executablePath)
                    ptimes = win32process.GetProcessTimes(handle)
                    meminfo = win32process.GetProcessMemoryInfo(handle)
                    pagefile = meminfo['PagefileUsage'] / (1024 * 1024)
                    workset = meminfo['WorkingSetSize'] / (1024 * 1024)
                    p[str(pid)] = (ptimes['UserTime'], ptimes['KernelTime'],
                                   pagefile, workset, filename, executablePath)

                    total += float(ptimes['UserTime']) + float(
                        ptimes['KernelTime'])

                    handle.Close()

                except Exception as myException:
                    self.logger.error(myException)

        p['total'] = total

        return p
Ejemplo n.º 14
0
def run():
    batch_size = 32
    nPlane = 40
    nDepth = 4
    #nDepth      = 3
    width = 28
    height = 28
    symbol = get_symbol(nDepth)

    param_names = [
        name for name in symbol.list_arguments()
        if name not in ['data', 'softmax_label']
    ]
    data_shapes = {
        'data': (batch_size, nPlane, width, height),
        'softmax_label': (batch_size, )
    }
    train_exec = _bind_exec(symbol,
                            mx.cpu(),
                            data_shapes,
                            param_names,
                            need_grad=True,
                            base_exec=None,
                            shared_data_arrays={})

    logging.getLogger().setLevel(logging.INFO)
    for i in xrange(10000):
        data_source = array(
            numpy.random.binomial(1, 0.5, batch_size * nPlane * width *
                                  height).reshape(batch_size, nPlane, width,
                                                  height))
        label_source = array(numpy.random.choice(width * height, batch_size))
        data_source.copyto(train_exec.arg_dict['data'])
        label_source.copyto(train_exec.arg_dict['softmax_label'])
        train_exec.forward(is_train=True)
        memUsed = float(
            win32process.GetProcessMemoryInfo(-1)['PagefileUsage']) / (1024. *
                                                                       1024.)
        logging.info('Memory used: %s MB' % memUsed)
Ejemplo n.º 15
0
 def memory():
     process_handle = win32api.GetCurrentProcess()
     memory_info = win32process.GetProcessMemoryInfo(process_handle)
     return memory_info['PeakWorkingSetSize']
Ejemplo n.º 16
0
 def get_current_process_memory_usage() -> float:
     """Get current process memory usage in MB."""
     d = win32process.GetProcessMemoryInfo(win32process.GetCurrentProcess())  # type: ignore
     return 1.0 * d["WorkingSetSize"] / 1024 ** 2
Ejemplo n.º 17
0
    si.dwFlags = win32process.STARTF_USESHOWWINDOW | win32con.STARTF_USESTDHANDLES
    cmd=" ".join(argv)
    win32api.SetLastError(0)
    #PyHANDLE, PyHANDLE, int, int = CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo )
    try:
        pi= win32process.CreateProcess(None,cmd,None,None,True,win32con.CREATE_SUSPENDED,None,RootDir,si)
    except pywintypes.error , e:
        ret[1]=e[2]
        return ret
    #pi  (hProcess, hThread, dwProcessId, dwThreadId)
    win32process.ResumeThread(pi[1]) #hThread
    win32api.CloseHandle(pi[1])

    while True:
        exitCode=win32process.GetExitCodeProcess(pi[0])
        pmc=win32process.GetProcessMemoryInfo(pi[0])
        ft=win32process.GetProcessTimes(pi[0])
        ret[3]=mem = round(float(pmc["PeakPagefileUsage"]) / 1024 / 1024,2)
        ret[2]=time= round(float(ft["UserTime"]) / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,2)
        print exitCode,ret
        if limit is not None and time>limit[0]:
            ret[1]=u"超过时间限制"
            CloseProcess(pi)
            return ret
        if limit is not None and mem>limit[1]:
            ret[1]=u"超过内存限制"
            CloseProcess(pi)
            return ret
        if exitCode != 259:
            if not exitCode:
                ret[0]=True
Ejemplo n.º 18
0
# Process size in XP from Python
import win32process
print win32process.GetProcessMemoryInfo(win32process.GetCurrentProcess())
Ejemplo n.º 19
0
    def log(self):
        freq = 0
        target_period = None

        if self.target_freq is not None:
            target_period = 1 / self.target_freq

        while True:
            start = time.time()
            print("\rLogging freq: " + str(round(freq, 2)) + " Hz", end="")

            mem = psutil.virtual_memory()

            data = {}
            data["timestamp"] = datetime.datetime.now().isoformat()
            data["%cpu"] = psutil.cpu_percent()
            data["mem_available"] = mem.available
            data["mem_used"] = mem.used
            data["processes"] = []
            data["focussed_window"] = None

            win_informations = []
            win32gui.EnumWindows(self.__window_callback, win_informations)

            for id in win32process.EnumProcesses():
                process = {}
                process["pid"] = id
                try:
                    p_handle = win32api.OpenProcess(
                        win32con.PROCESS_ALL_ACCESS, False, id)

                    process["cmd"] = win32process.GetModuleFileNameEx(
                        p_handle, 0)

                    times = win32process.GetProcessTimes(p_handle)
                    process["start"] = times["CreationTime"].isoformat()
                    process["ktime"] = (times["KernelTime"] * 100) / 1000
                    process["utime"] = (times["UserTime"] * 100) / 1000

                    etime = (datetime.datetime.now(tz=None) -
                             times["CreationTime"].replace(tzinfo=None)
                             ).total_seconds()
                    ktime = (process["ktime"] / (10**6))
                    cpu_util = round(ktime / etime * 100, 2)
                    process["%cpu"] = cpu_util

                    mem_info = win32process.GetProcessMemoryInfo(p_handle)
                    process["mem"] = mem_info["WorkingSetSize"]
                except Exception as e:
                    pass

                # print(process)
                data["processes"].append(process)

            for window in win_informations:
                if window["focus"]:
                    data["focussed_window"] = window
                    break

            self.out_file.write(json.dumps(data) + "\n")
            self.out_file.flush()

            period = time.time() - start

            if target_period is not None:
                time.sleep(max(target_period - period, 0))

            freq = 1 / (time.time() - start)
Ejemplo n.º 20
0
 def memory_info(self):
     return win32process.GetProcessMemoryInfo(self.phandle)
Ejemplo n.º 21
0
 def meminfo(self, handle):
     return win32process.GetProcessMemoryInfo(handle)
Ejemplo n.º 22
0
def debug_memory_win32api(message='', short=True):
    """Use trace.note() to dump the running memory info."""
    from breezy import trace
    if has_ctypes:

        class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
            """Used by GetProcessMemoryInfo"""
            _fields_ = [
                ('cb', ctypes.c_ulong),
                ('PageFaultCount', ctypes.c_ulong),
                ('PeakWorkingSetSize', ctypes.c_size_t),
                ('WorkingSetSize', ctypes.c_size_t),
                ('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
                ('QuotaPagedPoolUsage', ctypes.c_size_t),
                ('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
                ('QuotaNonPagedPoolUsage', ctypes.c_size_t),
                ('PagefileUsage', ctypes.c_size_t),
                ('PeakPagefileUsage', ctypes.c_size_t),
                ('PrivateUsage', ctypes.c_size_t),
            ]

        cur_process = ctypes.windll.kernel32.GetCurrentProcess()
        mem_struct = PROCESS_MEMORY_COUNTERS_EX()
        ret = ctypes.windll.psapi.GetProcessMemoryInfo(
            cur_process, ctypes.byref(mem_struct), ctypes.sizeof(mem_struct))
        if not ret:
            trace.note(gettext('Failed to GetProcessMemoryInfo()'))
            return
        info = {
            'PageFaultCount': mem_struct.PageFaultCount,
            'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
            'WorkingSetSize': mem_struct.WorkingSetSize,
            'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
            'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
            'QuotaPeakNonPagedPoolUsage':
            mem_struct.QuotaPeakNonPagedPoolUsage,
            'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
            'PagefileUsage': mem_struct.PagefileUsage,
            'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
            'PrivateUsage': mem_struct.PrivateUsage,
        }
    elif has_win32api:
        import win32process
        # win32process does not return PrivateUsage, because it doesn't use
        # PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
        proc = win32process.GetCurrentProcess()
        info = win32process.GetProcessMemoryInfo(proc)
    else:
        trace.note(
            gettext('Cannot debug memory on win32 without ctypes'
                    ' or win32process'))
        return
    if short:
        # using base-2 units (see HACKING.txt).
        trace.note(
            gettext('WorkingSize {0:>7}KiB'
                    '\tPeakWorking {1:>7}KiB\t{2}').format(
                        info['WorkingSetSize'] / 1024,
                        info['PeakWorkingSetSize'] / 1024, message))
        return
    if message:
        trace.note('%s', message)
    trace.note(gettext('WorkingSize       %8d KiB'),
               info['WorkingSetSize'] / 1024)
    trace.note(gettext('PeakWorking       %8d KiB'),
               info['PeakWorkingSetSize'] / 1024)
    trace.note(gettext('PagefileUsage     %8d KiB'),
               info.get('PagefileUsage', 0) / 1024)
    trace.note(gettext('PeakPagefileUsage %8d KiB'),
               info.get('PeakPagefileUsage', 0) / 1024)
    trace.note(gettext('PrivateUsage      %8d KiB'),
               info.get('PrivateUsage', 0) / 1024)
    trace.note(gettext('PageFaultCount    %8d'), info.get('PageFaultCount', 0))