Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
fun.py 4.01 KiB
import multiprocessing as mp
import os
import time

import pandas as pd
import numpy as np
import yaml


def main():
    # sort_kem_alg_via_categories_type()
    tryout_pool_with_shared_array()


def init_worker(condition, shared_array):
    global namespace_condition
    global acquired_network_namespaces
    namespace_condition = condition
    acquired_network_namespaces = shared_array


def work(id, protocol, kem_alg, n_measurements):
    def aquire_network_namespace():
        with namespace_condition:
            while True:
                print(
                    f"{id}: Before acquiring", [i for i in acquired_network_namespaces]
                )
                for i in range(1, len(acquired_network_namespaces) + 1):
                    if not acquired_network_namespaces[i - 1]:
                        acquired_network_namespaces[i - 1] = True
                        print(f"{id}: Acquired network namespace {i}")
                        print(
                            f"{id}: After acquiring",
                            [i for i in acquired_network_namespaces],
                        )
                        return i
                # make this process sleep until another wakes him up
                namespace_condition.wait()

    def release_network_namespace(i):
        with namespace_condition:
            print(f"{id}: Before releasing", [i for i in acquired_network_namespaces])
            acquired_network_namespaces[i - 1] = False
            print(f"{id}: Released network namespace {i}")
            print(f"{id}: After releasing", [i for i in acquired_network_namespaces])
            # wake another processes that are sleeping
            namespace_condition.notify()

    network_namespace = aquire_network_namespace()

    print(f"{id}: measuring now", protocol, kem_alg)
    time.sleep(5)

    release_network_namespace(network_namespace)

    return id, protocol, kem_alg, n_measurements


def tryout_pool_with_shared_array():
    POOL_SIZE = 3
    NETWORK_NAMESPACES = 2

    condition = mp.Condition()
    shared_array = mp.Array("b", [False] * NETWORK_NAMESPACES)
    with mp.Pool(
        processes=POOL_SIZE,
        initializer=init_worker,
        initargs=(
            condition,
            shared_array,
        ),
    ) as pool:
        output = pool.starmap(
            work, [(i, "protocol", "kem_alg", 5) for i in range(1, (POOL_SIZE * 2) + 1)]
        )
        print(output)


def sort_kem_alg_via_categories_type():
    df = pd.read_feather("feathers/data.feather")
    print(df.info())

    categories = [
        "secp256r1",
        "secp384r1",
        "secp521r1",
        "x25519",
        "x448",
        "mlkem512",
        "p256_mlkem512",
        "x25519_mlkem512",
        "mlkem768",
        "p384_mlkem768",
        "x448_mlkem768",
        "x25519_mlkem768",
        "p256_mlkem768",
        "mlkem1024",
        "p521_mlkem1024",
        "p384_mlkem1024",
        "bikel1",
        "p256_bikel1",
        "x25519_bikel1",
        "bikel3",
        "p384_bikel3",
        "x448_bikel3",
        "bikel5",
        "p521_bikel5",
        "hqc128",
        "p256_hqc128",
        "x25519_hqc128",
        "hqc192",
        "p384_hqc192",
        "x448_hqc192",
        "hqc256",
        "p521_hqc256",
        "frodo640aes",
        "p256_frodo640aes",
        "x25519_frodo640aes",
        "frodo640shake",
        "p256_frodo640shake",
        "x25519_frodo640shake",
        "frodo976aes",
        "p384_frodo976aes",
        "x448_frodo976aes",
        "frodo976shake",
        "p384_frodo976shake",
        "x448_frodo976shake",
        "frodo1344aes",
        "p521_frodo1344aes",
        "frodo1344shake",
        "p521_frodo1344shake",
    ]
    df["kem_alg"] = pd.Categorical(df["kem_alg"], categories=categories, ordered=True)
    # df.to_feather("feathers/data.feather")

    subpart = df.query(
        "scenario == 'corrupt' and protocol == 'quic' and srv_corrupt == 1.0"
    )
    subpart = subpart[["scenario", "protocol", "kem_alg", "srv_corrupt"]]
    print(subpart.sort_values(by=["kem_alg"]))


main()