[Phi] adding basic host emulation for development without access to a
Mirror Gitea refs to GitHub / mirror (push) Successful in 39s
Test / build_and_test (push) Successful in 5m15s
Build / build (push) Successful in 7m20s

Xeon Phi
This commit is contained in:
2026-08-02 12:46:59 +02:00
parent fa673251c0
commit e68d78cccc
18 changed files with 512 additions and 213 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ static PhiStatus ExecuteCommand(PhiCommandReader* reader, const PhiCmdHeader* co
return PHI_STATUS_BAD_MESSAGE;
}
int HandleWorkExecution(scif_epd_t endpoint, const PhiMessageHeader* header)
int HandleWorkExecution(PhiEndpoint endpoint, const PhiMessageHeader* header)
{
PhiWorkExecutionRequest request;
PhiWorkExecutionReply reply = {
+2 -2
View File
@@ -5,11 +5,11 @@
typedef struct PhiCommandReader
{
scif_epd_t endpoint;
PhiEndpoint endpoint;
uint64_t remaining;
} PhiCommandReader;
int HandleWorkExecution(scif_epd_t endpoint, const PhiMessageHeader* header);
int HandleWorkExecution(PhiEndpoint endpoint, const PhiMessageHeader* header);
int PhiDrainCommandReader(PhiCommandReader* reader);
PhiStatus PhiReadCommandData(PhiCommandReader* reader, void* data, uint64_t size);
+17 -33
View File
@@ -3,7 +3,7 @@
#include <Logger.h>
#include <Memory.h>
static int HandleHello(scif_epd_t endpoint, const PhiMessageHeader* header)
static int HandleHello(PhiEndpoint endpoint, const PhiMessageHeader* header)
{
PhiHelloRequest request;
PhiHelloReply reply = {
@@ -32,41 +32,25 @@ static int HandleHello(scif_epd_t endpoint, const PhiMessageHeader* header)
return SendReply(endpoint, header, &reply, sizeof(reply));
}
scif_epd_t StartDaemon()
PhiEndpoint StartDaemon(void)
{
PhiLogInfo("Starting the daemon...");
scif_epd_t endpoint = scif_open();
if(endpoint < 0)
{
PhiLogError("Failed to create SCIF endpoint");
return 0;
}
if(scif_bind(endpoint, PHI_SCIF_PORT) < 0)
{
PhiLogError("Failed to bind SCIF port");
scif_close(endpoint);
return 0;
}
if(scif_listen(endpoint, 16) < 0)
{
PhiLogError("Could not listen to SCIF port");
scif_close(endpoint);
return 0;
}
PhiEndpoint endpoint = PhiTransportListen(PHI_TRANSPORT_PORT);
if(endpoint == PHI_ENDPOINT_INVALID)
PhiLogError("Could not listen on the Phi transport");
PhiLogInfo("Daemon started");
return endpoint;
}
void ShutdownDaemon(scif_epd_t endpoint)
void ShutdownDaemon(PhiEndpoint endpoint)
{
PhiLogInfo("Shuting down the daemon...");
scif_close(endpoint);
PhiLogInfo("Shutting down the daemon...");
PhiTransportClose(endpoint);
}
int HandlePacket(scif_epd_t endpoint)
int HandlePacket(PhiEndpoint endpoint)
{
for(;;)
{
@@ -123,14 +107,14 @@ int HandlePacket(scif_epd_t endpoint)
}
}
int ReadAll(scif_epd_t endpoint, void* data, size_t size)
int ReadAll(PhiEndpoint endpoint, void* data, size_t size)
{
uint8_t* bytes = data;
size_t offset = 0;
while(offset < size)
{
int got = scif_recv(endpoint, bytes + offset, size - offset, SCIF_RECV_BLOCK);
ssize_t got = PhiTransportReceive(endpoint, bytes + offset, size - offset);
if(got <= 0)
return -1;
offset += (size_t)got;
@@ -139,14 +123,14 @@ int ReadAll(scif_epd_t endpoint, void* data, size_t size)
return 0;
}
int WriteAll(scif_epd_t endpoint, const void* data, size_t size)
int WriteAll(PhiEndpoint endpoint, const void* data, size_t size)
{
const uint8_t* bytes = data;
size_t offset = 0;
while(offset < size)
{
int sent = scif_send(endpoint, (void*)(bytes + offset), size - offset, SCIF_SEND_BLOCK);
ssize_t sent = PhiTransportSend(endpoint, bytes + offset, size - offset);
if(sent <= 0)
return -1;
offset += (size_t)sent;
@@ -155,7 +139,7 @@ int WriteAll(scif_epd_t endpoint, const void* data, size_t size)
return 0;
}
int SendReply(scif_epd_t endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size)
int SendReply(PhiEndpoint endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size)
{
PhiMessageHeader reply = {
.magic = PHI_PROTOCOL_MAGIC,
@@ -171,7 +155,7 @@ int SendReply(scif_epd_t endpoint, const PhiMessageHeader* request, const void*
return WriteAll(endpoint, payload, (size_t)payload_size);
}
int SendStatus(scif_epd_t endpoint, const PhiMessageHeader* request, PhiStatus status)
int SendStatus(PhiEndpoint endpoint, const PhiMessageHeader* request, PhiStatus status)
{
PhiFreeMemoryReply reply = {
.result = {
@@ -183,7 +167,7 @@ int SendStatus(scif_epd_t endpoint, const PhiMessageHeader* request, PhiStatus s
return SendReply(endpoint, request, &reply, sizeof(reply));
}
int DrainPayload(scif_epd_t endpoint, uint64_t size)
int DrainPayload(PhiEndpoint endpoint, uint64_t size)
{
uint8_t buffer[256];
+9 -10
View File
@@ -3,18 +3,17 @@
#include <sys/types.h>
#include <scif.h>
#include <Protocol.h>
#include <Transport.h>
scif_epd_t StartDaemon();
void ShutdownDaemon(scif_epd_t endpoint);
PhiEndpoint StartDaemon(void);
void ShutdownDaemon(PhiEndpoint endpoint);
int DrainPayload(scif_epd_t endpoint, uint64_t size);
int HandlePacket(scif_epd_t endpoint);
int ReadAll(scif_epd_t endpoint, void* data, size_t size);
int SendReply(scif_epd_t endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size);
int SendStatus(scif_epd_t endpoint, const PhiMessageHeader* request, PhiStatus status);
int WriteAll(scif_epd_t endpoint, const void* data, size_t size);
int DrainPayload(PhiEndpoint endpoint, uint64_t size);
int HandlePacket(PhiEndpoint endpoint);
int ReadAll(PhiEndpoint endpoint, void* data, size_t size);
int SendReply(PhiEndpoint endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size);
int SendStatus(PhiEndpoint endpoint, const PhiMessageHeader* request, PhiStatus status);
int WriteAll(PhiEndpoint endpoint, const void* data, size_t size);
#endif
+1
View File
@@ -84,6 +84,7 @@ void PhiLog(PhiLogLevel level, const char* fmt, const char* file, const char* fu
va_end(argptr);
fputc('\n', out);
fflush(out);
if(level == PHI_LOG_LEVEL_FATAL)
{
+2 -2
View File
@@ -3,7 +3,7 @@
#include <Logger.h>
#include <Memory.h>
int HandleAllocMemory(scif_epd_t endpoint, const PhiMessageHeader* header)
int HandleAllocMemory(PhiEndpoint endpoint, const PhiMessageHeader* header)
{
PhiAllocMemoryRequest request;
PhiAllocMemoryReply reply = {
@@ -42,7 +42,7 @@ int HandleAllocMemory(scif_epd_t endpoint, const PhiMessageHeader* header)
return SendReply(endpoint, header, &reply, sizeof(reply));
}
int HandleFreeMemory(scif_epd_t endpoint, const PhiMessageHeader* header)
int HandleFreeMemory(PhiEndpoint endpoint, const PhiMessageHeader* header)
{
PhiFreeMemoryRequest request;
PhiFreeMemoryReply reply = {
+2 -2
View File
@@ -3,7 +3,7 @@
#include <Daemon.h>
int HandleAllocMemory(scif_epd_t endpoint, const PhiMessageHeader* header);
int HandleFreeMemory(scif_epd_t endpoint, const PhiMessageHeader* header);
int HandleAllocMemory(PhiEndpoint endpoint, const PhiMessageHeader* header);
int HandleFreeMemory(PhiEndpoint endpoint, const PhiMessageHeader* header);
#endif
+109
View File
@@ -0,0 +1,109 @@
#include <Transport.h>
#ifdef PHI_HOST_EMULATION
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
static struct sockaddr_in PhiLoopbackAddress(uint16_t port)
{
struct sockaddr_in address = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {
.s_addr = htonl(INADDR_LOOPBACK),
},
};
return address;
}
PhiEndpoint PhiTransportAccept(PhiEndpoint endpoint)
{
return accept(endpoint, NULL, NULL);
}
int PhiTransportClose(PhiEndpoint endpoint)
{
return close(endpoint);
}
PhiEndpoint PhiTransportListen(uint16_t port)
{
PhiEndpoint endpoint = socket(AF_INET, SOCK_STREAM, 0);
if(endpoint == PHI_ENDPOINT_INVALID)
return PHI_ENDPOINT_INVALID;
const int reuse_address = 1;
if(setsockopt(endpoint, SOL_SOCKET, SO_REUSEADDR, &reuse_address, sizeof(reuse_address)) < 0)
{
PhiTransportClose(endpoint);
return PHI_ENDPOINT_INVALID;
}
const struct sockaddr_in address = PhiLoopbackAddress(port);
if(bind(endpoint, (const struct sockaddr*)&address, sizeof(address)) < 0 || listen(endpoint, 16) < 0)
{
PhiTransportClose(endpoint);
return PHI_ENDPOINT_INVALID;
}
return endpoint;
}
ssize_t PhiTransportReceive(PhiEndpoint endpoint, void* data, size_t size)
{
return recv(endpoint, data, size, 0);
}
ssize_t PhiTransportSend(PhiEndpoint endpoint, const void* data, size_t size)
{
#ifdef MSG_NOSIGNAL
return send(endpoint, data, size, MSG_NOSIGNAL);
#else
return send(endpoint, data, size, 0);
#endif
}
#else
PhiEndpoint PhiTransportAccept(PhiEndpoint endpoint)
{
struct scif_portID peer;
PhiEndpoint client = PHI_ENDPOINT_INVALID;
if(scif_accept(endpoint, &peer, &client, SCIF_ACCEPT_SYNC) < 0)
return PHI_ENDPOINT_INVALID;
return client;
}
int PhiTransportClose(PhiEndpoint endpoint)
{
return scif_close(endpoint);
}
PhiEndpoint PhiTransportListen(uint16_t port)
{
PhiEndpoint endpoint = scif_open();
if(endpoint == PHI_ENDPOINT_INVALID)
return PHI_ENDPOINT_INVALID;
if(scif_bind(endpoint, port) < 0 || scif_listen(endpoint, 16) < 0)
{
PhiTransportClose(endpoint);
return PHI_ENDPOINT_INVALID;
}
return endpoint;
}
ssize_t PhiTransportReceive(PhiEndpoint endpoint, void* data, size_t size)
{
return scif_recv(endpoint, data, size, SCIF_RECV_BLOCK);
}
ssize_t PhiTransportSend(PhiEndpoint endpoint, const void* data, size_t size)
{
return scif_send(endpoint, (void*)data, size, SCIF_SEND_BLOCK);
}
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef APE_PHI_TRANSPORT_H
#define APE_PHI_TRANSPORT_H
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#ifdef PHI_HOST_EMULATION
typedef int PhiEndpoint;
#else
#include <scif.h>
typedef scif_epd_t PhiEndpoint;
#endif
#define PHI_ENDPOINT_INVALID ((PhiEndpoint) - 1)
PhiEndpoint PhiTransportAccept(PhiEndpoint endpoint);
int PhiTransportClose(PhiEndpoint endpoint);
PhiEndpoint PhiTransportListen(uint16_t port);
ssize_t PhiTransportReceive(PhiEndpoint endpoint, void* data, size_t size);
ssize_t PhiTransportSend(PhiEndpoint endpoint, const void* data, size_t size);
#endif
+25 -11
View File
@@ -2,24 +2,37 @@
#include <pthread.h>
#include <stdint.h>
#ifdef PHI_HOST_EMULATION
#include <string.h>
#include <unistd.h>
#endif
#include <Daemon.h>
#include <Logger.h>
static void* HandleClient(void* const argument)
{
scif_epd_t client = (scif_epd_t)(intptr_t)argument;
PhiEndpoint client = (PhiEndpoint)(intptr_t)argument;
(void)HandlePacket(client);
scif_close(client);
PhiTransportClose(client);
return NULL;
}
int main(void)
int main(int argc, char** argv)
{
scif_epd_t endpoint = StartDaemon();
#ifdef PHI_HOST_EMULATION
if(argc == 2 && strcmp(argv[1], "--unlink-on-start") == 0)
(void)unlink(argv[0]);
#else
(void)argc;
(void)argv;
#endif
PhiEndpoint endpoint = StartDaemon();
pthread_attr_t client_thread_attributes;
if(endpoint == 0)
if(endpoint == PHI_ENDPOINT_INVALID)
return 1;
if(pthread_attr_init(&client_thread_attributes) != 0 ||
@@ -32,22 +45,23 @@ int main(void)
for(;;)
{
struct scif_portID peer;
scif_epd_t client;
PhiEndpoint client = PhiTransportAccept(endpoint);
if(scif_accept(endpoint, &peer, &client, SCIF_ACCEPT_SYNC) < 0)
if(client == PHI_ENDPOINT_INVALID)
{
if(errno == EINTR)
continue;
PhiLogError("Could not accept SCIF connection");
PhiLogError("Could not accept transport connection");
break;
}
PhiLogInfo("Host connected to the daemon");
pthread_t client_thread;
if(pthread_create(&client_thread, &client_thread_attributes, HandleClient, (void*)(intptr_t)client) != 0)
{
PhiLogError("Could not create SCIF client thread");
scif_close(client);
PhiLogError("Could not create transport client thread");
PhiTransportClose(client);
}
}