github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/server/wimpy/wimpy.go (about) 1 /* 2 * MIT License 3 * 4 * Copyright (c) 2023 EASL and the vHive community 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 package main 26 27 import ( 28 "context" 29 "errors" 30 "fmt" 31 util "github.com/eth-easl/loader/pkg/common" 32 "github.com/eth-easl/loader/pkg/workload/proto" 33 "net" 34 "time" 35 36 log "github.com/sirupsen/logrus" 37 "golang.org/x/sys/unix" 38 "google.golang.org/grpc" 39 "google.golang.org/grpc/reflection" 40 ) 41 42 type funcServer struct { 43 proto.UnimplementedExecutorServer 44 } 45 46 func (s *funcServer) Execute(ctx context.Context, req *proto.FaasRequest) (*proto.FaasReply, error) { 47 start := time.Now() 48 runtimeRequested := req.RuntimeInMilliSec 49 timeoutSem := time.After(time.Duration(runtimeRequested) * time.Millisecond) 50 if runtimeRequested <= 0 { 51 //* Some of the durations were incorrectly recorded as 0 in the trace. 52 return &proto.FaasReply{}, errors.New("non-positive execution time") 53 } 54 55 pageSize := unix.Getpagesize() 56 numPagesRequested := util.Mib2b(req.MemoryInMebiBytes) / uint32(pageSize) 57 //* Golang internally uses `mmap()`, talking to OS directly. 58 pages, err := unix.Mmap(-1, 0, int(numPagesRequested)*int(numPagesRequested), 59 unix.PROT_WRITE, unix.MAP_ANON|unix.MAP_PRIVATE) 60 if err != nil { 61 log.Errorf("Failed to allocate requested memory: %v", err) 62 return &proto.FaasReply{}, err 63 } 64 err = unix.Munmap(pages) //* Don't even touch the allocated pages -> let them stay virtaul memory. 65 util.Check(err) 66 67 <-timeoutSem //* Blocking wait. 68 return &proto.FaasReply{ 69 Message: "Wimpy func -- DONE", // Unused 70 DurationInMicroSec: uint32(time.Since(start).Microseconds()), 71 MemoryUsageInKb: util.B2Kib(numPagesRequested * uint32(pageSize)), 72 }, nil 73 } 74 75 func main() { 76 serverPort := 80 77 78 lis, err := net.Listen("tcp", fmt.Sprintf(":%d", serverPort)) 79 if err != nil { 80 log.Fatalf("failed to listen: %v", err) 81 } 82 83 funcServer := &funcServer{} 84 grpcServer := grpc.NewServer() 85 reflection.Register(grpcServer) // gRPC Server Reflection is used by gRPC CLI. 86 proto.RegisterExecutorServer(grpcServer, funcServer) 87 err = grpcServer.Serve(lis) 88 util.Check(err) 89 }