github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/tools/trace_synthesizer/synthesizer.py (about) 1 # MIT License 2 # 3 # Copyright (c) 2023 EASL and the vHive community 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in all 13 # copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 # SOFTWARE. 22 23 import logging 24 import random 25 import string 26 27 import numpy as np 28 29 from util import * 30 31 32 def hash_generator(size): 33 chars = string.ascii_lowercase + string.digits 34 return ''.join(random.choice(chars) for _ in range(size)) 35 36 37 def generate(args): 38 functions = args.functions 39 beginning = args.beginning 40 target = args.target 41 step = args.step 42 duration = args.duration 43 execution = args.execution 44 memory = args.memory 45 output_path = args.output 46 mode = args.mode 47 logging.basicConfig(filename='synthesizer.log', level=logging.DEBUG, force=True) 48 inv_df = load_data("base_traces/inv.csv") 49 mem_df = load_data("base_traces/mem.csv") 50 run_df = load_data("base_traces/run.csv") 51 52 hashFunction = [] 53 hashOwner = [] 54 hashApp = [] 55 56 lenHashes = 64 57 sampleCount = 1 58 # needs to be > 0 to work with loader implementation on loader_unit_tests branch 59 # see https://github.com/eth-easl/loader/blob/acdcde214d7a08d3603011ec5c9d28885ab3e986/pkg/generator/specification.go#L189 60 for i in range(functions): 61 hashFunction.append(hash_generator(lenHashes)) 62 hashOwner.append(hash_generator(lenHashes)) 63 hashApp.append(hash_generator(lenHashes)) 64 65 mem = [memory] 66 mem = np.repeat(mem, len(mem_df.columns) - 4) 67 run = [execution] 68 run = np.repeat(run, len(run_df.columns) - 5) 69 70 for i in range(functions): 71 memArr = [hashApp[i], hashOwner[i], hashFunction[i], sampleCount] 72 memArr.extend(mem) 73 mem_df.loc[len(mem_df)] = memArr 74 runArr = [hashFunction[i], hashOwner[i], hashApp[i], execution, sampleCount] 75 runArr.extend(run) 76 run_df.loc[len(run_df)] = runArr 77 invArr = [hashApp[i], hashFunction[i], hashOwner[i]] 78 if mode == 0: 79 rps = [*range(beginning, target + 1, step)] 80 ipm = [60 * x for x in rps] # convert rps to invocations per minute 81 ipm = np.repeat(ipm, duration) 82 # pad with zeros to get trace that is 1440 minutes 83 ipm = np.pad(ipm, (0, 1440 - len(ipm)), 'constant') 84 85 invArr.extend(ipm) 86 elif mode == 1: 87 padding = 10 88 89 p = [0] * padding 90 positionOf1 = int(i / (functions / padding)) 91 p[positionOf1] = 1 92 93 repetitions = int(duration / padding) 94 pattern = p * repetitions 95 96 invArr.extend(pattern) 97 else: 98 p = [1, 0, 0] 99 100 repetitions = int(duration / len(p)) 101 pattern = p * repetitions 102 103 invArr.extend(pattern) 104 105 inv_df.loc[len(inv_df)] = invArr 106 107 p1 = f"{output_path}/invocations.csv" 108 save_data(inv_df, p1) 109 logging.info(f"saved invocations to {p1}") 110 p2 = f"{output_path}/memory.csv" 111 save_data(mem_df, p2) 112 logging.info(f"saved invocations to {p2}") 113 p3 = f"{output_path}/durations.csv" 114 save_data(run_df, p3) 115 logging.info(f"saved invocations to {p3}") 116 117 return inv_df, mem_df, run_df