|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Intel Corporation |
| 3 | + * |
| 4 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 5 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + */ |
| 7 | + |
| 8 | +#include <arpa/inet.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | +#include <sys/socket.h> |
| 13 | +#include <unistd.h> |
| 14 | + |
| 15 | +#include <umf/ipc.h> |
| 16 | +#include <umf/memory_pool.h> |
| 17 | +#include <umf/pools/pool_scalable.h> |
| 18 | +#include <umf/providers/provider_os_memory.h> |
| 19 | + |
| 20 | +#define INET_ADDR "127.0.0.1" |
| 21 | +#define CONSUMER_MSG_SIZE 256 |
| 22 | +#define RECV_BUFF_SIZE 1024 |
| 23 | + |
| 24 | +// consumer's response message |
| 25 | +#define CONSUMER_MSG \ |
| 26 | + "This is the consumer. I just wrote a new number directly into your " \ |
| 27 | + "shared memory!" |
| 28 | + |
| 29 | +int consumer_connect(int port) { |
| 30 | + struct sockaddr_in consumer_addr; |
| 31 | + struct sockaddr_in producer_addr; |
| 32 | + int producer_addr_len; |
| 33 | + int producer_socket = -1; |
| 34 | + int consumer_socket = -1; |
| 35 | + int ret = -1; |
| 36 | + |
| 37 | + // create a socket |
| 38 | + consumer_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 39 | + if (consumer_socket < 0) { |
| 40 | + fprintf(stderr, "[consumer] ERROR: creating socket failed\n"); |
| 41 | + return -1; |
| 42 | + } |
| 43 | + |
| 44 | + fprintf(stderr, "[consumer] Socket created\n"); |
| 45 | + |
| 46 | + // set the IP address and the port |
| 47 | + consumer_addr.sin_family = AF_INET; |
| 48 | + consumer_addr.sin_port = htons(port); |
| 49 | + consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR); |
| 50 | + |
| 51 | + // bind to the IP address and the port |
| 52 | + if (bind(consumer_socket, (struct sockaddr *)&consumer_addr, |
| 53 | + sizeof(consumer_addr)) < 0) { |
| 54 | + fprintf(stderr, "[consumer] ERROR: cannot bind to the port\n"); |
| 55 | + goto err_close_consumer_socket; |
| 56 | + } |
| 57 | + |
| 58 | + fprintf(stderr, "[consumer] Binding done\n"); |
| 59 | + |
| 60 | + // listen for the producer |
| 61 | + if (listen(consumer_socket, 1) < 0) { |
| 62 | + fprintf(stderr, "[consumer] ERROR: listen() failed\n"); |
| 63 | + goto err_close_consumer_socket; |
| 64 | + } |
| 65 | + |
| 66 | + fprintf(stderr, "[consumer] Listening for incoming connections ...\n"); |
| 67 | + |
| 68 | + // accept an incoming connection |
| 69 | + producer_addr_len = sizeof(producer_addr); |
| 70 | + producer_socket = accept(consumer_socket, (struct sockaddr *)&producer_addr, |
| 71 | + (socklen_t *)&producer_addr_len); |
| 72 | + if (producer_socket < 0) { |
| 73 | + fprintf(stderr, "[consumer] ERROR: accept() failed\n"); |
| 74 | + goto err_close_consumer_socket; |
| 75 | + } |
| 76 | + |
| 77 | + fprintf(stderr, "[consumer] Producer connected at IP %s and port %i\n", |
| 78 | + inet_ntoa(producer_addr.sin_addr), ntohs(producer_addr.sin_port)); |
| 79 | + |
| 80 | + ret = producer_socket; // success |
| 81 | + |
| 82 | +err_close_consumer_socket: |
| 83 | + close(consumer_socket); |
| 84 | + |
| 85 | + return ret; |
| 86 | +} |
| 87 | + |
| 88 | +int main(int argc, char *argv[]) { |
| 89 | + char consumer_message[CONSUMER_MSG_SIZE]; |
| 90 | + char recv_buffer[RECV_BUFF_SIZE]; |
| 91 | + int producer_socket; |
| 92 | + int ret = -1; |
| 93 | + |
| 94 | + if (argc < 2) { |
| 95 | + fprintf(stderr, "usage: %s port\n", argv[0]); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + int port = atoi(argv[1]); |
| 100 | + |
| 101 | + umf_memory_provider_handle_t OS_memory_provider = NULL; |
| 102 | + umf_os_memory_provider_params_t os_params; |
| 103 | + enum umf_result_t umf_result; |
| 104 | + |
| 105 | + os_params = umfOsMemoryProviderParamsDefault(); |
| 106 | + os_params.visibility = UMF_MEM_MAP_SHARED; |
| 107 | + |
| 108 | + // create OS memory provider |
| 109 | + umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &os_params, |
| 110 | + &OS_memory_provider); |
| 111 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 112 | + fprintf(stderr, |
| 113 | + "[consumer] ERROR: creating OS memory provider failed\n"); |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + umf_memory_pool_handle_t scalable_pool; |
| 118 | + umf_result = umfPoolCreate(umfScalablePoolOps(), OS_memory_provider, NULL, |
| 119 | + 0, &scalable_pool); |
| 120 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 121 | + fprintf(stderr, |
| 122 | + "[producer] ERROR: creating jemalloc UMF pool failed\n"); |
| 123 | + goto err_destroy_OS_memory_provider; |
| 124 | + } |
| 125 | + |
| 126 | + producer_socket = consumer_connect(port); |
| 127 | + if (producer_socket < 0) { |
| 128 | + goto err_destroy_scalable_pool; |
| 129 | + } |
| 130 | + |
| 131 | + // receive the IPC handle from the producer's |
| 132 | + ssize_t len = recv(producer_socket, recv_buffer, RECV_BUFF_SIZE, 0); |
| 133 | + if (len < 0) { |
| 134 | + fprintf(stderr, "[consumer] ERROR: recv() failed\n"); |
| 135 | + goto err_close_producer_socket; |
| 136 | + } |
| 137 | + |
| 138 | + umf_ipc_handle_t IPC_handle = (umf_ipc_handle_t)recv_buffer; |
| 139 | + |
| 140 | + fprintf( |
| 141 | + stderr, |
| 142 | + "[consumer] Received the IPC handle from the producer (%zi bytes)\n", |
| 143 | + len); |
| 144 | + |
| 145 | + void *SHM_ptr; |
| 146 | + umf_result = umfOpenIPCHandle(scalable_pool, IPC_handle, &SHM_ptr); |
| 147 | + if (umf_result == UMF_RESULT_ERROR_NOT_SUPPORTED) { |
| 148 | + fprintf(stderr, |
| 149 | + "[consumer] SKIP: opening the IPC handle is not supported\n"); |
| 150 | + ret = 1; // SKIP |
| 151 | + |
| 152 | + // write the SKIP response to the consumer_message buffer |
| 153 | + strcpy(consumer_message, "SKIP"); |
| 154 | + |
| 155 | + // send the SKIP response to the producer |
| 156 | + send(producer_socket, consumer_message, strlen(consumer_message) + 1, |
| 157 | + 0); |
| 158 | + |
| 159 | + goto err_close_producer_socket; |
| 160 | + } |
| 161 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 162 | + fprintf(stderr, "[consumer] ERROR: opening the IPC handle failed\n"); |
| 163 | + goto err_close_producer_socket; |
| 164 | + } |
| 165 | + |
| 166 | + fprintf(stderr, |
| 167 | + "[consumer] Opened the IPC handle received from the producer\n"); |
| 168 | + |
| 169 | + // read the current value from the shared memory |
| 170 | + unsigned long long SHM_number_1 = *(unsigned long long *)SHM_ptr; |
| 171 | + fprintf( |
| 172 | + stderr, |
| 173 | + "[consumer] Read the number from the producer's shared memory: %llu\n", |
| 174 | + SHM_number_1); |
| 175 | + |
| 176 | + // calculate the new value |
| 177 | + unsigned long long SHM_number_2 = SHM_number_1 / 2; |
| 178 | + |
| 179 | + // write the new number directly to the producer's shared memory |
| 180 | + *(unsigned long long *)SHM_ptr = SHM_number_2; |
| 181 | + fprintf(stderr, |
| 182 | + "[consumer] Wrote a new number directly to the producer's shared " |
| 183 | + "memory: %llu\n", |
| 184 | + SHM_number_2); |
| 185 | + |
| 186 | + // write the response to the consumer_message buffer |
| 187 | + memset(consumer_message, 0, sizeof(consumer_message)); |
| 188 | + strcpy(consumer_message, CONSUMER_MSG); |
| 189 | + |
| 190 | + // send response to the producer |
| 191 | + if (send(producer_socket, consumer_message, strlen(consumer_message) + 1, |
| 192 | + 0) < 0) { |
| 193 | + fprintf(stderr, "[consumer] ERROR: send() failed\n"); |
| 194 | + goto err_CloseIPCHandle; |
| 195 | + } |
| 196 | + |
| 197 | + fprintf(stderr, "[consumer] Sent a response message to the producer\n"); |
| 198 | + |
| 199 | + ret = 0; // SUCCESS |
| 200 | + |
| 201 | +err_CloseIPCHandle: |
| 202 | + umf_result = umfCloseIPCHandle(SHM_ptr); |
| 203 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 204 | + fprintf(stderr, "[consumer] ERROR: closing the IPC handle failed\n"); |
| 205 | + } |
| 206 | + |
| 207 | + fprintf(stderr, |
| 208 | + "[consumer] Closed the IPC handle received from the producer\n"); |
| 209 | + |
| 210 | +err_close_producer_socket: |
| 211 | + close(producer_socket); |
| 212 | + |
| 213 | +err_destroy_scalable_pool: |
| 214 | + umfPoolDestroy(scalable_pool); |
| 215 | + |
| 216 | +err_destroy_OS_memory_provider: |
| 217 | + umfMemoryProviderDestroy(OS_memory_provider); |
| 218 | + |
| 219 | + if (ret == 0) { |
| 220 | + fprintf(stderr, "[consumer] Shutting down (status OK) ...\n"); |
| 221 | + } else if (ret == 1) { |
| 222 | + fprintf(stderr, "[consumer] Shutting down (status SKIP) ...\n"); |
| 223 | + ret = 0; |
| 224 | + } else { |
| 225 | + fprintf(stderr, "[consumer] Shutting down (status ERROR) ...\n"); |
| 226 | + } |
| 227 | + |
| 228 | + return ret; |
| 229 | +} |
0 commit comments