예제 #1
0
def worker_usage(session: Session, logger):
    provider = ComputerProvider(session)
    docker_provider = DockerProvider(session)

    computer = socket.gethostname()
    docker = docker_provider.get(computer, DOCKER_IMG)
    usages = []

    for _ in range(1 if MODE_ECONOMIC else 10):
        # noinspection PyProtectedMember
        memory = dict(psutil.virtual_memory()._asdict())

        usage = {
            'cpu':
            psutil.cpu_percent(),
            'disk':
            disk(ROOT_FOLDER)[1],
            'memory':
            memory['percent'],
            'gpu': [{
                'memory': g.memoryUtil * 100,
                'load': g.load * 100
            } for g in GPUtil.getGPUs()]
        }

        provider.current_usage(computer, usage)
        usages.append(usage)
        docker.last_activity = now()
        docker_provider.update()

        time.sleep(10 if MODE_ECONOMIC else 1)

    usage = json.dumps({'mean': dict_func(usages, np.mean)})
    provider.add(ComputerUsage(computer=computer, usage=usage, time=now()))
예제 #2
0
def worker_usage(session: Session, logger):
    provider = ComputerProvider(session)
    docker_provider = DockerProvider(session)

    computer = socket.gethostname()
    docker = docker_provider.get(computer, DOCKER_IMG)
    usages = []

    count = int(10 / WORKER_USAGE_INTERVAL)
    count = max(1, count)

    for _ in range(count):
        # noinspection PyProtectedMember
        memory = dict(psutil.virtual_memory()._asdict())

        try:
            gpus = GPUtil.getGPUs()
        except ValueError as err:
            logger.info(f"Active GPUs not found: {err}")
            gpus = []

        usage = {
            'cpu':
            psutil.cpu_percent(),
            'disk':
            disk(ROOT_FOLDER)[1],
            'memory':
            memory['percent'],
            'gpu': [{
                'memory': g.memoryUtil * 100,
                'load': g.load * 100
            } for g in gpus]
        }

        provider.current_usage(computer, usage)
        usages.append(usage)
        docker.last_activity = now()
        docker_provider.update()

        time.sleep(WORKER_USAGE_INTERVAL)

    usage = json.dumps({'mean': dict_func(usages, np.mean)})
    provider.add(ComputerUsage(computer=computer, usage=usage, time=now()))