Dev/server malloc check (#1023)

* usleep in communication to actually relay the err message of memory allocation to the client (weird but test for now), function in server to handle memory allcoation issues (updates mess, ret and sendsit to the client and returns prior from function implementatin, setting fnum in client for the speicific functions that send to detector each argument separtely, they need to remember the fnum else they throw with the incorrect fnum
* server: every malloc must check if it succeeded, rearranging so that the free is clear as well (only in funcs so far)
* fixed malloc checks in other places other than funcs.c
This commit is contained in:
maliakal_d 2024-11-18 09:46:21 +01:00 committed by GitHub
parent 5088e5a205
commit e1497f9cb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 353 additions and 282 deletions

View File

@ -51,12 +51,12 @@ int dataBytes = 0;
int analogDataBytes = 0;
int digitalDataBytes = 0;
int transceiverDataBytes = 0;
char *analogData = 0;
char *digitalData = 0;
char *transceiverData = 0;
char volatile *analogDataPtr = 0;
char volatile *digitalDataPtr = 0;
char volatile *transceiverDataPtr = 0;
char *analogData = NULL;
char *digitalData = NULL;
char *transceiverData = NULL;
char volatile *analogDataPtr = NULL;
char volatile *digitalDataPtr = NULL;
char volatile *transceiverDataPtr = NULL;
char udpPacketData[UDP_PACKET_DATA_BYTES + sizeof(sls_detector_header)];
uint32_t adcEnableMask_1g = BIT32_MSK;
// 10g readout
@ -475,21 +475,15 @@ void setupDetector() {
analogDataBytes = 0;
digitalDataBytes = 0;
transceiverDataBytes = 0;
if (analogData) {
free(analogData);
analogData = 0;
}
if (digitalData) {
free(digitalData);
digitalData = 0;
}
if (transceiverData) {
free(transceiverData);
transceiverData = 0;
}
analogDataPtr = 0;
digitalDataPtr = 0;
transceiverData = 0;
free(analogData);
analogData = NULL;
free(digitalData);
digitalData = NULL;
free(transceiverData);
transceiverData = NULL;
analogDataPtr = NULL;
digitalDataPtr = NULL;
transceiverData = NULL;
{
for (int i = 0; i < NUM_CLOCKS; ++i) {
clkPhase[i] = 0;
@ -640,22 +634,15 @@ int updateDatabytesandAllocateRAM() {
return FAIL;
}
// clear RAM
if (analogData) {
free(analogData);
analogData = 0;
}
if (digitalData) {
free(digitalData);
digitalData = 0;
}
if (transceiverData) {
free(transceiverData);
transceiverData = 0;
}
free(analogData);
analogData = NULL;
free(digitalData);
digitalData = NULL;
free(transceiverData);
transceiverData = NULL;
// allocate RAM
if (analogDataBytes) {
analogData = malloc(analogDataBytes);
// cannot malloc
if (analogData == NULL) {
LOG(logERROR, ("Can not allocate analog data RAM for even 1 frame. "
"Probable cause: Memory Leak.\n"));
@ -665,7 +652,6 @@ int updateDatabytesandAllocateRAM() {
}
if (digitalDataBytes) {
digitalData = malloc(digitalDataBytes);
// cannot malloc
if (digitalData == NULL) {
LOG(logERROR,
("Can not allocate digital data RAM for even 1 frame. "
@ -677,7 +663,6 @@ int updateDatabytesandAllocateRAM() {
}
if (transceiverDataBytes) {
transceiverData = malloc(transceiverDataBytes);
// cannot malloc
if (transceiverData == NULL) {
LOG(logERROR,
("Can not allocate transceiver data RAM for even 1 frame. "

View File

@ -46,17 +46,27 @@ int Beb_deactivated_left_datastream = 1;
int Beb_deactivated_right_datastream = 1;
int Beb_deactivated_num_destinations = 1;
void Beb_Beb() {
int Beb_Beb() {
Beb_send_ndata = 0;
Beb_send_buffer_size = 1026;
Beb_send_data_raw =
malloc((Beb_send_buffer_size + 1) * sizeof(unsigned int));
if (Beb_send_data_raw == NULL) {
LOG(logERROR, ("Could not allocate memory for beb (send_data_raw)\n"));
return 0;
}
Beb_send_data = &Beb_send_data_raw[1];
Beb_recv_ndata = 0;
Beb_recv_buffer_size = 1026;
Beb_recv_data_raw =
malloc((Beb_recv_buffer_size + 1) * sizeof(unsigned int));
if (Beb_recv_data_raw == NULL) {
LOG(logERROR, ("Could not allocate memory for beb (recv_data_raw)\n"));
return 0;
}
Beb_recv_data = &Beb_recv_data_raw[1];
udp_header = (struct udp_header_type){
@ -83,6 +93,7 @@ void Beb_Beb() {
Beb_ClearHeaderData(1);
Beb_bit_mode = 4;
return 1;
}
void Beb_ClearHeaderData(int ten_gig) {

View File

@ -6,7 +6,7 @@
#include "slsDetectorServer_defs.h"
#include <stdlib.h>
void Beb_Beb();
int Beb_Beb();
void Beb_ClearHeaderData(int ten_gig);
int Beb_SetUpUDPHeader(unsigned int header_number, int ten_gig,
uint64_t src_mac, uint32_t src_ip, uint16_t src_port,

View File

@ -57,11 +57,19 @@ int Feb_Control_FebControl(int normal) {
Feb_Control_externalEnableMode = 0;
Feb_Control_subFrameMode = 0;
Feb_Control_trimbit_size = 263680;
Feb_Control_last_downloaded_trimbits =
malloc(Feb_Control_trimbit_size * sizeof(int));
if (Feb_Control_last_downloaded_trimbits == NULL) {
LOG(logERROR,
("Could not allocate memory for last downloaded trimbits\n"));
return 0;
}
Feb_Control_normal = normal;
Feb_Interface_SetAddress(Feb_Control_rightAddress, Feb_Control_leftAddress);
if (!Feb_Interface_SetAddress(Feb_Control_rightAddress,
Feb_Control_leftAddress))
return 0;
if (Feb_Control_activated) {
return Feb_Interface_SetByteOrder();
}

View File

@ -22,34 +22,57 @@ unsigned int Feb_Interface_recv_buffer_size;
unsigned int *Feb_Interface_recv_data_raw;
unsigned int *Feb_Interface_recv_data;
void Feb_Interface_FebInterface() {
int Feb_Interface_FebInterface() {
ll = &ll_local;
Feb_Interface_nfebs = 0;
Feb_Interface_feb_numb = 0;
Feb_Interface_send_ndata = 0;
Feb_Interface_send_buffer_size = 1026;
Feb_Interface_send_data_raw =
malloc((Feb_Interface_send_buffer_size + 1) * sizeof(unsigned int));
if (Feb_Interface_send_data_raw == NULL) {
LOG(logERROR,
("Could not allocate memory for feb interface (send_data_raw)\n"));
return 0;
}
Feb_Interface_send_data = &Feb_Interface_send_data_raw[1];
Feb_Interface_recv_ndata = 0;
Feb_Interface_recv_buffer_size = 1026;
Feb_Interface_recv_data_raw =
malloc((Feb_Interface_recv_buffer_size + 1) * sizeof(unsigned int));
if (Feb_Interface_recv_data_raw == NULL) {
LOG(logERROR,
("Could not allocate memory for feb interface (recv_data_raw)\n"));
return 0;
}
Feb_Interface_recv_data = &Feb_Interface_recv_data_raw[1];
Local_LocalLinkInterface(
ll, XPAR_PLB_LL_FIFO_AURORA_DUAL_CTRL_FEB_RIGHT_BASEADDR);
return 1;
}
void Feb_Interface_SetAddress(unsigned int leftAddr, unsigned int rightAddr) {
if (Feb_Interface_feb_numb)
free(Feb_Interface_feb_numb);
int Feb_Interface_SetAddress(unsigned int leftAddr, unsigned int rightAddr) {
free(Feb_Interface_feb_numb);
Feb_Interface_nfebs = 2;
Feb_Interface_feb_numb = malloc(2 * sizeof(unsigned int));
if (Feb_Interface_feb_numb == NULL) {
LOG(logERROR,
("Could not allocate memory for feb interface (feb_numb)\n"));
Feb_Interface_nfebs = 0;
return 0;
}
Feb_Interface_feb_numb[0] = leftAddr;
Feb_Interface_feb_numb[1] = rightAddr;
return 1;
}
int Feb_Interface_WriteTo(unsigned int ch) {

View File

@ -4,8 +4,8 @@
int Feb_Interface_WriteTo(unsigned int ch);
int Feb_Interface_ReadFrom(unsigned int ch, unsigned int ntrys);
void Feb_Interface_FebInterface();
void Feb_Interface_SetAddress(unsigned int leftAddr, unsigned int rightAddr);
int Feb_Interface_FebInterface();
int Feb_Interface_SetAddress(unsigned int leftAddr, unsigned int rightAddr);
int Feb_Interface_SetByteOrder();
int Feb_Interface_ReadRegister(unsigned int sub_num, unsigned int reg_num,
unsigned int *value_read);

View File

@ -662,7 +662,15 @@ int checkCommandLineConfiguration() {
#ifndef VIRTUAL
void setupFebBeb() {
sharedMemory_lockLocalLink();
Feb_Interface_FebInterface();
if (!Feb_Interface_FebInterface()) {
initError = FAIL;
sprintf(initErrorMessage,
"Could not intitalize eiger detector sever: feb interface\n");
LOG(logERROR, (initErrorMessage));
initCheckDone = 1;
sharedMemory_unlockLocalLink();
return;
}
if (!Feb_Control_FebControl(normal)) {
initError = FAIL;
sprintf(initErrorMessage,
@ -686,7 +694,14 @@ void setupFebBeb() {
LOG(logDEBUG1, ("%s server: FEB Initialization done\n",
isControlServer ? "Control" : "Stop"));
Beb_SetTopVariable(top);
Beb_Beb();
if (!Beb_Beb()) {
initError = FAIL;
sprintf(initErrorMessage,
"Could not intitalize eiger detector sever: beb\n");
LOG(logERROR, (initErrorMessage));
initCheckDone = 1;
return;
}
LOG(logDEBUG1, ("%s server: BEB Initialization done\n",
isControlServer ? "Control" : "Stop"));
@ -724,17 +739,23 @@ void setupFebBeb() {
}
#endif
void allocateDetectorStructureMemory() {
LOG(logINFO, ("This Server is for 1 Eiger half module (250k)\n\n"));
// Allocation of memory
int allocateDetectorStructureMemory() {
detectorModules = malloc(sizeof(sls_detector_module));
detectorChans = malloc(NCHIP * NCHAN * sizeof(int));
detectorDacs = malloc(NDAC * sizeof(int));
if (detectorModules == NULL || detectorChans == NULL ||
detectorDacs == NULL) {
initError = FAIL;
strcpy(initErrorMessage,
"Could not allocate memory for dacs or channels in detector\n");
LOG(logERROR, (initErrorMessage));
return FAIL;
}
LOG(logDEBUG1,
("modules from 0x%x to 0x%x\n", detectorModules, detectorModules));
LOG(logDEBUG1, ("chans from 0x%x to 0x%x\n", detectorChans, detectorChans));
LOG(logDEBUG1, ("dacs from 0x%x to 0x%x\n", detectorDacs, detectorDacs));
detectorModules->dacs = detectorDacs;
detectorModules->chanregs = detectorChans;
detectorModules->ndac = NDAC;
@ -748,14 +769,21 @@ void allocateDetectorStructureMemory() {
detectorModules->eV[2] = -1;
thisSettings = UNINITIALIZED;
// if trimval requested, should return -1 to acknowledge unknown
// initialize (trimbits at -1 for unknown)
for (int idac = 0; idac < (detectorModules)->ndac; ++idac) {
detectorDacs[idac] = 0;
}
for (int ichan = 0; ichan < (detectorModules->nchan); ichan++) {
*((detectorModules->chanregs) + ichan) = -1;
}
return OK;
}
void setupDetector() {
allocateDetectorStructureMemory();
LOG(logINFO, ("This Server is for 1 Eiger half module (250k)\n\n"));
if (allocateDetectorStructureMemory() == FAIL)
return;
// force top or master if in config file
if (readConfigFile() == FAIL)

View File

@ -3326,11 +3326,12 @@ int *getBadChannels(int *numChannels) {
if (*numChannels > 0) {
// get list of bad channels
retvals = malloc(*numChannels * sizeof(int));
memset(retvals, 0, *numChannels * sizeof(int));
if (retvals == NULL) {
LOG(logERROR, ("Could not allocate memory to get bad channels\n"));
*numChannels = -1;
return NULL;
}
memset(retvals, 0, *numChannels * sizeof(int));
int chIndex = 0;
int numAddr = MASK_STRIP_NUM_REGS;
// loop through registers

View File

@ -404,18 +404,24 @@ void initStopServer() {
/* set up detector */
void allocateDetectorStructureMemory() {
// Allocation of memory
int allocateDetectorStructureMemory() {
detectorModules = malloc(sizeof(sls_detector_module));
detectorChans = malloc(NCHAN_PER_MODULE * sizeof(int));
badChannelMask = malloc(NCHAN_PER_MODULE * sizeof(char));
memset(badChannelMask, 0, NCHAN_PER_MODULE * sizeof(char));
detectorDacs = malloc(NDAC * sizeof(int));
badChannelMask = malloc(NCHAN_PER_MODULE * sizeof(char));
if (detectorModules == NULL || detectorChans == NULL ||
detectorDacs == NULL || badChannelMask == NULL) {
initError = FAIL;
strcpy(initErrorMessage, "Could not allocate memory for dacs, channels "
"or bad channel mask in detector\n");
LOG(logERROR, (initErrorMessage));
return FAIL;
}
LOG(logDEBUG1,
("modules from 0x%x to 0x%x\n", detectorModules, detectorModules));
LOG(logDEBUG1, ("chans from 0x%x to 0x%x\n", detectorChans, detectorChans));
LOG(logDEBUG1, ("dacs from 0x%x to 0x%x\n", detectorDacs, detectorDacs));
(detectorModules)->dacs = detectorDacs;
(detectorModules)->chanregs = detectorChans;
(detectorModules)->ndac = NDAC;
@ -429,21 +435,22 @@ void allocateDetectorStructureMemory() {
(detectorModules)->eV[2] = 0;
thisSettings = UNINITIALIZED;
// initialize dacs
// initialize
for (int idac = 0; idac < (detectorModules)->ndac; ++idac) {
detectorDacs[idac] = 0;
}
// trimbits start at 0
for (int ichan = 0; ichan < (detectorModules->nchan); ichan++) {
*((detectorModules->chanregs) + ichan) = 0;
}
memset(badChannelMask, 0, NCHAN_PER_MODULE * sizeof(char));
return OK;
}
void setupDetector() {
LOG(logINFO, ("This Server is for 1 Mythen3 module \n"));
allocateDetectorStructureMemory();
if (allocateDetectorStructureMemory() == FAIL)
return;
if (checkCommandLineConfiguration() == FAIL)
return;
@ -1412,6 +1419,10 @@ int setTrimbits(int *trimbits) {
int setAllTrimbits(int val) {
LOG(logINFO, ("Setting all trimbits to %d\n", val));
int *trimbits = malloc(sizeof(int) * ((detectorModules)->nchan));
if (trimbits == NULL) {
LOG(logERROR, ("Could not allocate memory to set all trimbits\n"));
return FAIL;
}
for (int ichan = 0; ichan < ((detectorModules)->nchan); ++ichan) {
trimbits[ichan] = val;
}
@ -2469,11 +2480,12 @@ int *getBadChannels(int *numChannels) {
}
if (*numChannels > 0) {
retvals = malloc(*numChannels * sizeof(int));
memset(retvals, 0, *numChannels * sizeof(int));
if (retvals == NULL) {
LOG(logERROR, ("Could not allocate memory to get bad channels\n"));
*numChannels = -1;
return NULL;
}
memset(retvals, 0, *numChannels * sizeof(int));
// return only 1 channel for all counters
int ich = 0;
for (int i = 0; i != NCHAN_PER_MODULE; i = i + NCOUNTERS) {

View File

@ -140,7 +140,7 @@ void checkVirtual9MFlag();
void setupFebBeb();
#endif
#if defined(EIGERD) || defined(MYTHEN3D)
void allocateDetectorStructureMemory();
int allocateDetectorStructureMemory();
#endif
void setupDetector();
#if defined(CHIPTESTBOARDD)

View File

@ -11,6 +11,8 @@
// initialization functions
int updateModeAllowedFunction(int file_des);
int printSocketReadError();
int sendError(int file_des);
void setMemoryAllocationErrorMessage();
void init_detector();
int decode_function(int);
const char *getRetName();

View File

@ -590,13 +590,16 @@ int Server_SendResult(int fileDes, intType itype, void *retval,
sendData(fileDes, &ret1, sizeof(ret1), INT32);
if (ret == FAIL) {
// send error message
if (strlen(mess))
if (strlen(mess)) {
sendData(fileDes, mess, MAX_STR_LENGTH, OTHER);
usleep(0); // test
}
// debugging feature. should not happen.
else
else {
LOG(logERROR, ("No error message provided for this failure in %s "
"server. Will mess up TCP.\n",
(isControlServer ? "control" : "stop")));
}
}
// send return value
sendData(fileDes, retval, retvalSize, itype);

View File

@ -115,6 +115,28 @@ int printSocketReadError() {
return FAIL;
}
int sendError(int file_des) {
ret = FAIL;
LOG(logERROR, (mess));
Server_SendResult(file_des, INT32, NULL, 0);
return ret;
}
void setMemoryAllocationErrorMessage() {
struct sysinfo info;
sysinfo(&info);
sprintf(
mess,
"Memory allocation error (%s). Available space: %d MB. Please reboot",
getFunctionNameFromEnum((enum detFuncs)fnum),
(int)(info.freeram / (1024 * 1024)));
#ifdef EIGERD
strcat(mess, ".\n");
#else
strcat(mess, " using sls_detector_put rebootcontroller.\n");
#endif
}
void init_detector() {
memset(udpDetails, 0, sizeof(udpDetails));
#ifdef VIRTUAL
@ -1728,67 +1750,45 @@ int get_module(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
sls_detector_module module;
int *myDac = NULL;
int *myChan = NULL;
module.dacs = NULL;
module.chanregs = NULL;
#if !defined(MYTHEN3D) && !defined(EIGERD)
functionNotImplemented();
return Server_SendResult(file_des, INT32, NULL, 0);
#else
// allocate to receive module structure
// allocate dacs
myDac = malloc(getNumberOfDACs() * sizeof(int));
// error
if (getNumberOfDACs() > 0 && myDac == NULL) {
ret = FAIL;
sprintf(mess, "Could not allocate dacs\n");
LOG(logERROR, (mess));
} else
module.dacs = myDac;
// allocate chans
if (ret == OK) {
myChan = malloc(getTotalNumberOfChannels() * sizeof(int));
if (getTotalNumberOfChannels() > 0 && myChan == NULL) {
ret = FAIL;
strcpy(mess, "Could not allocate chans\n");
LOG(logERROR, (mess));
} else
module.chanregs = myChan;
int ndac = getNumberOfDACs();
int nchan = getTotalNumberOfChannels();
if (ndac <= 0 || nchan <= 0) {
strcpy(mess, "Invalid number of dacs/channels to set module\n");
return sendError(file_des);
}
// receive module structure
if (ret == OK) {
module.nchip = getNumberOfChips();
module.nchan = getTotalNumberOfChannels();
module.ndac = getNumberOfDACs();
// ensure nchan is not 0, else trimbits not copied
if (module.nchan == 0) {
strcpy(mess, "Could not get module as the number of channels to "
"copy is 0\n");
LOG(logERROR, (mess));
return FAIL;
}
getModule(&module);
}
#endif
Server_SendResult(file_des, INT32, NULL, 0);
if (ret != FAIL) {
if (sendModule(file_des, &module) < 0) {
ret = FAIL;
strcpy(mess, "Could not send module data\n");
LOG(logERROR, (mess));
}
}
if (myChan != NULL)
free(myChan);
if (myDac != NULL)
sls_detector_module module;
module.dacs = NULL;
module.chanregs = NULL;
int *myDac = malloc(ndac * sizeof(int));
int *myChan = malloc(nchan * sizeof(int));
if (myDac == NULL || myChan == NULL) {
free(myDac);
free(myChan);
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
module.dacs = myDac;
module.ndac = ndac;
module.chanregs = myChan;
module.nchan = nchan;
module.nchip = getNumberOfChips();
getModule(&module);
Server_SendResult(file_des, INT32, NULL, 0);
if (ret == OK && sendModule(file_des, &module) < 0) {
strcpy(mess, "Could not send module data\n");
ret = FAIL;
LOG(logERROR, (mess));
}
free(myChan);
free(myDac);
return ret;
#endif
}
int set_module(int file_des) {
@ -1798,63 +1798,53 @@ int set_module(int file_des) {
#if !(defined(MYTHEN3D) || defined(EIGERD))
functionNotImplemented();
#else
int ndac = getNumberOfDACs();
int nchan = getTotalNumberOfChannels();
if (ndac <= 0 || nchan <= 0) {
strcpy(mess, "Invalid number of dacs/channels to set module\n");
return sendError(file_des);
}
sls_detector_module module;
int *myDac = NULL;
int *myChan = NULL;
module.dacs = NULL;
module.chanregs = NULL;
// allocate to receive arguments
// allocate dacs
myDac = malloc(getNumberOfDACs() * sizeof(int));
// error
if (getNumberOfDACs() > 0 && myDac == NULL) {
ret = FAIL;
strcpy(mess, "Could not allocate dacs\n");
LOG(logERROR, (mess));
} else
module.dacs = myDac;
// allocate chans
if (ret == OK) {
myChan = malloc(getTotalNumberOfChannels() * sizeof(int));
if (getTotalNumberOfChannels() > 0 && myChan == NULL) {
ret = FAIL;
strcpy(mess, "Could not allocate chans\n");
LOG(logERROR, (mess));
} else
module.chanregs = myChan;
int *myDac = malloc(ndac * sizeof(int));
int *myChan = malloc(nchan * sizeof(int));
if (myDac == NULL || myChan == NULL) {
free(myDac);
free(myChan);
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
// receive arguments
if (ret == OK) {
module.nchip = getNumberOfChips();
module.nchan = getTotalNumberOfChannels();
module.ndac = getNumberOfDACs();
int ts = receiveModule(file_des, &module);
if (ts < 0) {
free(myChan);
free(myDac);
return printSocketReadError();
}
LOG(logDEBUG1, ("module register is %d, nchan %d, nchip %d, "
"ndac %d, iodelay %d, tau %d, eV %d\n",
module.reg, module.nchan, module.nchip, module.ndac,
module.iodelay, module.tau, module.eV[0]));
// should at least have a dac
if (ts <= (int)sizeof(sls_detector_module)) {
ret = FAIL;
strcpy(mess, "Cannot set module. Received incorrect number of "
"dacs or channels\n");
LOG(logERROR, (mess));
}
module.dacs = myDac;
module.ndac = ndac;
module.chanregs = myChan;
module.nchan = nchan;
module.nchip = getNumberOfChips();
int ts = receiveModule(file_des, &module);
if (ts < 0) {
free(myChan);
free(myDac);
return printSocketReadError();
}
LOG(logDEBUG1, ("module register is %d, nchan %d, nchip %d, "
"ndac %d, iodelay %d, tau %d, eV %d\n",
module.reg, module.nchan, module.nchip, module.ndac,
module.iodelay, module.tau, module.eV[0]));
// should at least have a dac
if (ts <= (int)sizeof(sls_detector_module)) {
strcpy(mess, "Cannot set module. Received incorrect number of "
"dacs or channels\n");
free(myChan);
free(myDac);
return sendError(file_des);
}
// only set
if (ret == OK && Server_VerifyLock() == OK) {
if (Server_VerifyLock() == OK) {
// check index
// setsettings
// setsettings
#ifndef MYTHEN3D
// m3 uses reg for chip (not settings)
validate_settings((enum detectorSettings)(module.reg));
@ -1867,10 +1857,8 @@ int set_module(int file_des) {
#endif
LOG(logDEBUG1, ("Settings: %d\n", retval));
}
if (myChan != NULL)
free(myChan);
if (myDac != NULL)
free(myDac);
free(myChan);
free(myDac);
#endif
return Server_SendResult(file_des, INT32, NULL, 0);
@ -2957,6 +2945,7 @@ int get_frames_left(int file_des) {
retval = getNumFramesLeft();
LOG(logDEBUG1, ("retval num frames left %lld\n", (long long int)retval));
#endif
return Server_SendResult(file_des, INT64, &retval, sizeof(retval));
}
@ -6549,6 +6538,10 @@ int set_veto_photon(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
#ifndef GOTTHARD2D
functionNotImplemented();
#else
int args[2] = {-1, -1};
if (receiveData(file_des, args, sizeof(args), INT32) < 0)
return printSocketReadError();
@ -6556,14 +6549,17 @@ int set_veto_photon(int file_des) {
const int numChannels = args[1];
int *gainIndices = malloc(sizeof(int) * numChannels);
if (receiveData(file_des, gainIndices, sizeof(int) * numChannels, INT32) <
0) {
int *values = malloc(sizeof(int) * numChannels);
if (gainIndices == NULL || values == NULL) {
free(gainIndices);
return printSocketReadError();
free(values);
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
int *values = malloc(sizeof(int) * numChannels);
if (receiveData(file_des, values, sizeof(int) * numChannels, INT32) < 0) {
if ((receiveData(file_des, gainIndices, sizeof(int) * numChannels, INT32) <
0) ||
(receiveData(file_des, values, sizeof(int) * numChannels, INT32)) < 0) {
free(gainIndices);
free(values);
return printSocketReadError();
@ -6572,9 +6568,6 @@ int set_veto_photon(int file_des) {
LOG(logINFO, ("Setting Veto Photon: [chipIndex:%d, nch:%d]\n", chipIndex,
numChannels));
#ifndef GOTTHARD2D
functionNotImplemented();
#else
// only set
if (Server_VerifyLock() == OK) {
if (numChannels != NCHAN) {
@ -6621,66 +6614,60 @@ int set_veto_photon(int file_des) {
}
}
}
free(gainIndices);
free(values);
#endif
if (gainIndices != NULL) {
free(gainIndices);
}
if (values != NULL) {
free(values);
}
return Server_SendResult(file_des, INT32, NULL, 0);
}
int get_veto_photon(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int arg = -1;
int *retvals = NULL;
int *gainRetvals = NULL;
#ifndef GOTTHARD2D
functionNotImplemented();
return Server_SendResult(file_des, INT32, NULL, 0);
#else
int arg = -1;
if (receiveData(file_des, &arg, sizeof(arg), INT32) < 0)
return printSocketReadError();
LOG(logDEBUG1, ("Getting veto photon [chip Index:%d]\n", arg));
#ifndef GOTTHARD2D
functionNotImplemented();
#else
retvals = malloc(sizeof(int) * NCHAN);
gainRetvals = malloc(sizeof(int) * NCHAN);
int *retvals = malloc(sizeof(int) * NCHAN);
int *gainRetvals = malloc(sizeof(int) * NCHAN);
if (gainRetvals == NULL || retvals == NULL) {
free(gainRetvals);
free(retvals);
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
memset(retvals, 0, sizeof(int) * NCHAN);
memset(gainRetvals, 0, sizeof(int) * NCHAN);
if (retvals == NULL || gainRetvals == NULL) {
// get only
int chipIndex = arg;
if (chipIndex < -1 || chipIndex >= NCHIP) {
ret = FAIL;
strcpy(
mess,
"Could not get veto photon. Could not allocate memory in server\n");
sprintf(mess, "Could not get veto photon. Invalid chip index %d\n",
chipIndex);
LOG(logERROR, (mess));
} else {
// get only
int chipIndex = arg;
if (chipIndex < -1 || chipIndex >= NCHIP) {
ret = FAIL;
sprintf(mess, "Could not get veto photon. Invalid chip index %d\n",
chipIndex);
ret = getVetoPhoton(chipIndex, retvals, gainRetvals);
if (ret == FAIL) {
strcpy(mess, "Could not get veto photon for chipIndex -1. Not the "
"same for all chips. Select specific chip index "
"instead.\n");
LOG(logERROR, (mess));
} else {
ret = getVetoPhoton(chipIndex, retvals, gainRetvals);
if (ret == FAIL) {
strcpy(mess,
"Could not get veto photon for chipIndex -1. Not the "
"same for all chips. Select specific chip index "
"instead.\n");
LOG(logERROR, (mess));
} else {
for (int i = 0; i < NCHAN; ++i) {
LOG(logDEBUG1,
("%d:[%d, %d]\n", i, retvals[i], gainRetvals[i]));
}
for (int i = 0; i < NCHAN; ++i) {
LOG(logDEBUG1,
("%d:[%d, %d]\n", i, retvals[i], gainRetvals[i]));
}
}
}
#endif
Server_SendResult(file_des, INT32, NULL, 0);
if (ret != FAIL) {
int nch = NCHAN;
@ -6688,13 +6675,10 @@ int get_veto_photon(int file_des) {
sendData(file_des, gainRetvals, sizeof(int) * NCHAN, INT32);
sendData(file_des, retvals, sizeof(int) * NCHAN, INT32);
}
if (retvals != NULL) {
free(retvals);
}
if (gainRetvals != NULL) {
free(gainRetvals);
}
free(retvals);
free(gainRetvals);
return ret;
#endif
}
int set_veto_reference(int file_des) {
@ -7864,16 +7848,19 @@ int set_pattern(int file_des) {
functionNotImplemented();
#else
patternParameters *pat = malloc(sizeof(patternParameters));
if (pat == NULL) {
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
memset(pat, 0, sizeof(patternParameters));
// ignoring endianness for eiger
if (receiveData(file_des, pat, sizeof(patternParameters), INT32) < 0) {
if (pat != NULL)
free(pat);
free(pat);
return printSocketReadError();
}
if (receiveData(file_des, args, MAX_STR_LENGTH, OTHER) < 0) {
if (pat != NULL)
free(pat);
free(pat);
return printSocketReadError();
}
@ -7881,8 +7868,7 @@ int set_pattern(int file_des) {
LOG(logDEBUG1, ("Setting Pattern from structure\n"));
ret = loadPattern(mess, logINFO, pat, args);
}
if (pat != NULL)
free(pat);
free(pat);
#endif
return Server_SendResult(file_des, INT32, NULL, 0);
@ -7918,6 +7904,10 @@ int get_pattern(int file_des) {
#else
patternParameters *pat = malloc(sizeof(patternParameters));
if (pat == NULL) {
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
memset(pat, 0, sizeof(patternParameters));
if (Server_VerifyLock() == OK) {
@ -7927,8 +7917,7 @@ int get_pattern(int file_des) {
// ignoring endianness for eiger
int ret =
Server_SendResult(file_des, INT32, pat, sizeof(patternParameters));
if (pat != NULL)
free(pat);
free(pat);
return ret;
#endif
}
@ -8036,10 +8025,13 @@ int set_scan(int file_des) {
if (ret == OK) {
scan = 1;
numScanSteps = (abs(stop - start) / abs(step)) + 1;
if (scanSteps != NULL) {
free(scanSteps);
}
// freed only at startup of the next scan
free(scanSteps);
scanSteps = malloc(numScanSteps * sizeof(int));
if (scanSteps == NULL) {
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
for (int i = 0; i != numScanSteps; ++i) {
scanSteps[i] = start + i * step;
LOG(logDEBUG1, ("scansteps[%d]:%d\n", i, scanSteps[i]));
@ -8313,55 +8305,54 @@ int set_adc_config(int file_des) {
int get_bad_channels(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int nretvals = 0;
int *retvals = NULL;
LOG(logDEBUG1, ("Getting bad channels\n"));
#if !defined(GOTTHARD2D) && !defined(MYTHEN3D)
functionNotImplemented();
return Server_SendResult(file_des, INT32, NULL, 0);
#else
// get only
retvals = getBadChannels(&nretvals);
int nretvals = 0;
int *retvals = getBadChannels(&nretvals);
if (nretvals == -1) {
ret = FAIL;
strcpy(mess, "Could not get bad channels. Memory allcoation error\n");
LOG(logERROR, (mess));
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
#endif
Server_SendResult(file_des, INT32, NULL, 0);
if (ret != FAIL) {
sendData(file_des, &nretvals, sizeof(nretvals), INT32);
if (nretvals > 0) {
sendData(file_des, retvals, sizeof(int) * nretvals, INT32);
}
}
if (retvals != NULL) {
free(retvals);
sendData(file_des, &nretvals, sizeof(nretvals), INT32);
if (nretvals > 0) {
sendData(file_des, retvals, sizeof(int) * nretvals, INT32);
}
free(retvals);
return ret;
#endif
}
int set_bad_channels(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
int nargs = 0;
int *args = NULL;
if (receiveData(file_des, &nargs, sizeof(nargs), INT32) < 0)
return printSocketReadError();
if (nargs > 0) {
args = malloc(nargs * sizeof(int));
if (receiveData(file_des, args, nargs * sizeof(int), INT32) < 0)
return printSocketReadError();
}
LOG(logDEBUG1, ("Setting %d bad channels\n", nargs));
#if !defined(GOTTHARD2D) && !defined(MYTHEN3D)
functionNotImplemented();
#else
int nargs = 0;
if (receiveData(file_des, &nargs, sizeof(nargs), INT32) < 0)
return printSocketReadError();
int *args = NULL;
if (nargs > 0) {
args = malloc(nargs * sizeof(int));
if (args == NULL) {
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
if (receiveData(file_des, args, nargs * sizeof(int), INT32) < 0) {
free(args);
return printSocketReadError();
}
}
LOG(logDEBUG1, ("Setting %d bad channels\n", nargs));
// only set
if (Server_VerifyLock() == OK) {
// validate bad channel number
@ -8390,11 +8381,11 @@ int set_bad_channels(int file_des) {
int nretvals = 0;
int *retvals = getBadChannels(&nretvals);
if (nretvals == -1) {
ret = FAIL;
strcpy(mess, "Could not get bad channels. Memory "
"allcoation error\n");
LOG(logERROR, (mess));
} else if (nretvals != nargs) {
free(args);
setMemoryAllocationErrorMessage();
return sendError(file_des);
}
if (nretvals != nargs) {
ret = FAIL;
sprintf(mess,
"Could not set bad channels. Set %d channels, but "
@ -8403,15 +8394,11 @@ int set_bad_channels(int file_des) {
nargs, nretvals);
LOG(logERROR, (mess));
}
if (retvals != NULL) {
free(retvals);
}
free(retvals);
}
}
}
if (args != NULL) {
free(args);
}
free(args);
#endif
return Server_SendResult(file_des, INT32, NULL, 0);
}
@ -9940,14 +9927,9 @@ void receive_program_via_blackfin(int file_des, enum PROGRAM_INDEX index,
src = malloc(MAX_BLACKFIN_PROGRAM_SIZE);
if (src == NULL) {
fclose(fd);
struct sysinfo info;
sysinfo(&info);
sprintf(mess,
"Could not %s. Memory allocation failure. Free "
"space: %d MB\n",
functionType, (int)(info.freeram / (1024 * 1024)));
LOG(logERROR, (mess));
setMemoryAllocationErrorMessage();
ret = FAIL;
LOG(logERROR, (mess));
}
}
Server_SendResult(file_des, INT32, NULL, 0);
@ -10090,6 +10072,7 @@ void receive_program_default(int file_des, enum PROGRAM_INDEX index,
}
Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) {
free(src);
return;
}

View File

@ -161,6 +161,7 @@ Module::getTypeFromDetector(const std::string &hostname, uint16_t cport) {
LOG(logDEBUG1) << "Getting Module type ";
ClientSocket socket("Detector", hostname, cport);
socket.Send(F_GET_DETECTOR_TYPE);
socket.setFnum(F_GET_DETECTOR_TYPE);
if (socket.Receive<int>() == FAIL) {
throw RuntimeError("Detector (" + hostname + ", " +
std::to_string(cport) +
@ -555,6 +556,7 @@ void Module::setSynchronization(const bool value) {
std::vector<int> Module::getBadChannels() const {
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_GET_BAD_CHANNELS);
client.setFnum(F_GET_BAD_CHANNELS);
if (client.Receive<int>() == FAIL) {
throw DetectorError("Detector " + std::to_string(moduleIndex) +
" returned error: " + client.readErrorMessage());
@ -576,6 +578,7 @@ void Module::setBadChannels(std::vector<int> list) {
LOG(logDEBUG1) << "Sending bad channels to detector, nch:" << nch;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_SET_BAD_CHANNELS);
client.setFnum(F_SET_BAD_CHANNELS);
client.Send(nch);
if (nch > 0) {
client.Send(list);
@ -966,6 +969,7 @@ std::vector<int64_t> Module::getFramesCaughtByReceiver() const {
if (shm()->useReceiverFlag) {
auto client = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
client.Send(F_GET_RECEIVER_FRAMES_CAUGHT);
client.setFnum(F_GET_RECEIVER_FRAMES_CAUGHT);
if (client.Receive<int>() == FAIL) {
throw ReceiverError(
"Receiver " + std::to_string(moduleIndex) +
@ -988,6 +992,7 @@ std::vector<int64_t> Module::getNumMissingPackets() const {
if (shm()->useReceiverFlag) {
auto client = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
client.Send(F_GET_NUM_MISSING_PACKETS);
client.setFnum(F_GET_NUM_MISSING_PACKETS);
if (client.Receive<int>() == FAIL) {
throw ReceiverError(
"Receiver " + std::to_string(moduleIndex) +
@ -1010,6 +1015,7 @@ std::vector<int64_t> Module::getReceiverCurrentFrameIndex() const {
if (shm()->useReceiverFlag) {
auto client = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
client.Send(F_GET_RECEIVER_FRAME_INDEX);
client.setFnum(F_GET_RECEIVER_FRAME_INDEX);
if (client.Receive<int>() == FAIL) {
throw ReceiverError(
"Receiver " + std::to_string(moduleIndex) +
@ -1726,6 +1732,7 @@ void Module::sendReceiverRateCorrections(const std::vector<int64_t> &t) {
<< ']';
auto receiver = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
receiver.Send(F_SET_RECEIVER_RATE_CORRECT);
receiver.setFnum(F_SET_RECEIVER_RATE_CORRECT);
receiver.Send(static_cast<int>(t.size()));
receiver.Send(t);
if (receiver.Receive<int>() == FAIL) {
@ -2022,6 +2029,7 @@ void Module::sendVetoPhoton(const int chipIndex,
const int args[]{chipIndex, nch};
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_SET_VETO_PHOTON);
client.setFnum(F_SET_VETO_PHOTON);
client.Send(args);
client.Send(gainIndices);
client.Send(values);
@ -2036,6 +2044,7 @@ void Module::getVetoPhoton(const int chipIndex,
LOG(logDEBUG1) << "Getting veto photon [" << chipIndex << "]\n";
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_GET_VETO_PHOTON);
client.setFnum(F_GET_VETO_PHOTON);
client.Send(chipIndex);
if (client.Receive<int>() == FAIL) {
throw DetectorError("Detector " + std::to_string(moduleIndex) +
@ -2542,6 +2551,7 @@ std::string Module::getPatterFileName() const {
void Module::setPattern(const Pattern &pat, const std::string &fname) {
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_SET_PATTERN);
client.setFnum(F_SET_PATTERN);
client.Send(pat.data(), pat.size());
char args[MAX_STR_LENGTH]{};
strcpy_safe(args, fname.c_str());
@ -2652,6 +2662,7 @@ std::map<std::string, std::string> Module::getAdditionalJsonHeader() const {
}
auto client = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
client.Send(F_GET_ADDITIONAL_JSON_HEADER);
client.setFnum(F_GET_ADDITIONAL_JSON_HEADER);
if (client.Receive<int>() == FAIL) {
throw ReceiverError("Receiver " + std::to_string(moduleIndex) +
" returned error: " + client.readErrorMessage());
@ -2697,6 +2708,7 @@ void Module::setAdditionalJsonHeader(
<< ToString(jsonHeader);
auto client = ReceiverSocket(shm()->rxHostname, shm()->rxTCPPort);
client.Send(F_SET_ADDITIONAL_JSON_HEADER);
client.setFnum(F_SET_ADDITIONAL_JSON_HEADER);
client.Send(size);
if (size > 0)
client.Send(&buff[0], buff.size());
@ -2892,6 +2904,7 @@ std::string Module::executeCommand(const std::string &cmd) {
<< "): Sending command " << cmd;
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_EXEC_COMMAND);
client.setFnum(F_EXEC_COMMAND);
client.Send(arg);
if (client.Receive<int>() == FAIL) {
std::cout << '\n';
@ -3504,6 +3517,7 @@ void Module::setModule(sls_detector_module &module, bool trimbits) {
}
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_SET_MODULE);
client.setFnum(F_SET_MODULE);
sendModule(&module, client);
if (client.Receive<int>() == FAIL) {
throw DetectorError("Module " + std::to_string(moduleIndex) +
@ -3516,6 +3530,7 @@ sls_detector_module Module::getModule() {
sls_detector_module module(shm()->detType);
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_GET_MODULE);
client.setFnum(F_GET_MODULE);
if (client.Receive<int>() == FAIL) {
throw DetectorError("Module " + std::to_string(moduleIndex) +
" returned error: " + client.readErrorMessage());