195 lines
5.1 KiB
C
195 lines
5.1 KiB
C
#include <math.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "scaler_misc.h"
|
|
#include "utils.h"
|
|
|
|
int my_snprintf(char* buf, size_t size, const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
int ret;
|
|
|
|
va_start(args, fmt);
|
|
ret = vsnprintf(buf, size, fmt, args);
|
|
va_end(args);
|
|
|
|
if (ret < 0) {
|
|
WARN("my_snprintf: encoding error for format: %s", fmt);
|
|
return ret;
|
|
}
|
|
if ((size_t)ret >= size) {
|
|
WARN("my_snprintf: output truncated for format: %s (needed %d bytes, "
|
|
"buffer size %zu)",
|
|
fmt, ret + 1, size);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Create a temporary file with a unique name using mkstemp.
|
|
* The file name is passed in as a parameter and should be a template
|
|
* like "/tmp/as_in_XXXXXX.json". The 'X's will be replaced with a unique
|
|
* sequence to ensure the file name is unique.
|
|
*
|
|
* Returns 0 on success, -1 on failure. file_name will contain the name of the
|
|
* created file.
|
|
*/
|
|
int create_temp_file(char* file_name)
|
|
{
|
|
// Call mkstemp to create a temporary file
|
|
int fd = mkstemp(file_name);
|
|
if (fd == -1) {
|
|
ERROR("Failed to create temporary file, errno %d : %s.", errno,
|
|
strerror(errno));
|
|
return -1;
|
|
}
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
char* my_putstr(char* s)
|
|
{
|
|
size_t len;
|
|
char* p;
|
|
if (!s) {
|
|
return s = "";
|
|
}
|
|
|
|
len = strlen(s);
|
|
p = (char*)calloc(len + 1, sizeof(char));
|
|
if (!p) {
|
|
SET_ERROR(ENOMEM);
|
|
ERROR("Memory allocation failed");
|
|
return NULL;
|
|
}
|
|
strcpy(p, s);
|
|
return p;
|
|
}
|
|
|
|
/*
|
|
* Safe string, return TRUE if both NULL or both equal, FALSE otherwise
|
|
*/
|
|
int my_strcmp(const char* p1, const char* p2)
|
|
{
|
|
if (!p1 && !p2)
|
|
return true;
|
|
if (!p1 || !p2)
|
|
return false;
|
|
return strcmp(p1, p2) == 0;
|
|
}
|
|
|
|
void print_demand_list(scaler_demand_array_t* d_list)
|
|
{
|
|
if (!d_list || d_list->num <= 0) {
|
|
return;
|
|
}
|
|
INFO("Got DB demand List:");
|
|
INFO("Number of demands: %d", d_list->num);
|
|
for (int i = 0; i < d_list->num; i++) {
|
|
scaler_demand_t* demand = &d_list->data[i];
|
|
INFO("Demand %d: provider=%s, template=%s, count=%d", i,
|
|
demand->tpl_id.provider_name, demand->tpl_id.template_id,
|
|
demand->count);
|
|
}
|
|
}
|
|
|
|
int cmp_template_id(scaler_template_id_t* p1, scaler_template_id_t* p2)
|
|
{
|
|
if (!p1 && !p2)
|
|
return true;
|
|
if (!p1 || !p2)
|
|
return false;
|
|
return my_strcmp(p1->provider_name, p2->provider_name)
|
|
&& my_strcmp(p1->template_id, p2->template_id);
|
|
}
|
|
|
|
void print_request(void* data)
|
|
{
|
|
scaler_request_t* request = (scaler_request_t*)data;
|
|
if (!request) {
|
|
return;
|
|
}
|
|
|
|
INFO("Request: req_id=%s, type=%d, status=%d, provider=%s, template=%s, "
|
|
"count=%d",
|
|
request->request_id ? request->request_id : "NULL", request->type,
|
|
request->status,
|
|
request->tpl_id.provider_name ? request->tpl_id.provider_name : "NULL",
|
|
request->tpl_id.template_id ? request->tpl_id.template_id : "NULL",
|
|
request->count);
|
|
}
|
|
|
|
static int _print_request_4_xlist(void* data, void* arg)
|
|
{
|
|
print_request(data);
|
|
return 0;
|
|
}
|
|
|
|
void print_request_list(xlist_t* r_list)
|
|
{
|
|
if (!r_list) {
|
|
return;
|
|
}
|
|
|
|
INFO("Got DB request List:");
|
|
INFO("Number of requests: %d", r_list->count);
|
|
|
|
xlist_iter(r_list, (xlist_iter_fn_t)_print_request_4_xlist, NULL);
|
|
}
|
|
|
|
void print_instance(void* p)
|
|
{
|
|
scaler_instance_t* inst = (scaler_instance_t*)p;
|
|
if (!inst) {
|
|
return;
|
|
}
|
|
|
|
INFO("Instance: id=%s, hostname=%s, private_ip=%s, public_ip=%s, "
|
|
"launch_time=%ld, alloc_status=%d, inst_state=%s, "
|
|
"provider=%s, template=%s, request_id=%s, relinquish_id=%s, "
|
|
"last_update_time=%ld",
|
|
inst->instance_id ? inst->instance_id : "NULL",
|
|
inst->hostname ? inst->hostname : "NULL",
|
|
inst->private_ip ? inst->private_ip : "NULL",
|
|
inst->public_ip ? inst->public_ip : "NULL", (long)inst->launch_time,
|
|
inst->alloc_status, inst->inst_state ? inst->inst_state : "NULL",
|
|
inst->tpl_id.provider_name ? inst->tpl_id.provider_name : "NULL",
|
|
inst->tpl_id.template_id ? inst->tpl_id.template_id : "NULL",
|
|
inst->request_id ? inst->request_id : "NULL",
|
|
inst->relinquish_id ? inst->relinquish_id : "NULL",
|
|
(long)inst->last_update_time);
|
|
}
|
|
|
|
static int _print_instance_4_xlist(void* data, void* arg)
|
|
{
|
|
print_instance(data);
|
|
return 0;
|
|
}
|
|
|
|
void print_instance_list(xlist_t* i_list)
|
|
{
|
|
if (!i_list || i_list->count <= 0) {
|
|
return;
|
|
}
|
|
INFO("Got DB instance List:");
|
|
INFO("Number of instances: %d", i_list->count);
|
|
xlist_iter(i_list, (xlist_iter_fn_t)_print_instance_4_xlist, NULL);
|
|
}
|
|
|
|
// Compare two floating point numbers for equality
|
|
// If the two numbers difference is within a small epsilon, consider them equal
|
|
#define DEFAULT_EPSILON 0.001
|
|
int float_cmp_ex(float a, float b, float ep)
|
|
{
|
|
|
|
if (fabs(a - b) < ep) {
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int float_cmp(float a, float b) { return float_cmp_ex(a, b, DEFAULT_EPSILON); }
|