github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/server/trace-func-py/trace_func.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 from concurrent import futures 24 import logging 25 import datetime 26 import grpc 27 import math 28 from os import getenv 29 from time import process_time_ns 30 from random import seed, randrange 31 from psutil import virtual_memory 32 from numpy import empty, float32 33 34 import faas_pb2 35 import faas_pb2_grpc 36 37 class Executor(faas_pb2_grpc.Executor): 38 39 def Execute(self, request, context, **kwargs): 40 start_time = datetime.datetime.now() 41 response = execute_function(request.input, request.runtime, request.memory) 42 elapsed = datetime.datetime.now() - start_time 43 elapsed_us = int(1000000 * elapsed.total_seconds()) 44 return faas_pb2.FaasReply(latency=elapsed_us, response=str(response)) 45 46 47 def execute_function(input, runTime, totalMem): 48 startTime = process_time_ns() 49 50 chunkSize = 2**10 # size of a kb or 1024 51 totalMem = totalMem*(2**10) # convert Mb to kb 52 memory = virtual_memory() 53 used = (memory.total - memory.available) // chunkSize # convert to kb 54 additional = max(1, (totalMem - used)) 55 array = empty(additional*chunkSize, dtype=float32) # make an uninitialized array of that size, uninitialized to keep it fast 56 # convert to ns 57 runTime = (runTime - 1)*(10**6) # -1 because it should be slighly bellow that runtime 58 memoryIndex = 0 59 while process_time_ns() - startTime < runTime: 60 for i in range(0, chunkSize): 61 sin_i = math.sin(i) 62 cos_i = math.cos(i) 63 sqrt_i = math.sqrt(i) 64 array[memoryIndex + i] = sin_i 65 memoryIndex = (memoryIndex + chunkSize) % additional*chunkSize 66 return (process_time_ns() - startTime) // 1000 67 68 def serve(): 69 server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) 70 faas_pb2_grpc.add_ExecutorServicer_to_server(Executor(), server) 71 server.add_insecure_port('[::]:80') 72 server.start() 73 server.wait_for_termination() 74 75 76 if __name__ == '__main__': 77 logging.basicConfig() 78 serve()