Skip to content

Commit

Permalink
Simplify some code and dump all known string periodically to /tmp/str…
Browse files Browse the repository at this point in the history
…ingdump.txt if debug.gc is true

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 13, 2024
1 parent 848367f commit 8634059
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ static void recycle(void)

log_debug(DEBUG_GC, "Recycled %u clients, %u domains, and %u cache records (scanned %u queries)",
clients_recycled, domains_recycled, cache_recycled, counters->queries);

dump_strings();
}
}

Expand Down Expand Up @@ -530,6 +532,8 @@ static bool check_files_on_same_device(const char *path1, const char *path2)

void *GC_thread(void *val)
{
(void)val; // Mark parameter as unused

// Set thread name
prctl(PR_SET_NAME, thread_names[GC], 0, 0, 0);

Expand Down
17 changes: 9 additions & 8 deletions src/lookup-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ static inline int cmp_hash(const uint32_t a, const uint32_t b)
* found element or NULL (!) if the element is not found which provides no
* information about the position where the element would need to be inserted.
*/
static bool binsearch(const struct lookup_table *base, const uint32_t hash,
size_t size, const struct lookup_table **try)
static bool binsearch(struct lookup_table *base, const uint32_t hash,
size_t size, struct lookup_table **try)
{
// Initialize the base pointer to the start of the array
*try = base;
Expand Down Expand Up @@ -207,7 +207,7 @@ bool lookup_insert(const enum memory_type type, const unsigned int id, const uin
// Find the correct position in the lookup_table array
// We do not check the return value as we are inserting a new element
// and don't care if elements with the same hash value exist already
const struct lookup_table *try = table;
struct lookup_table *try = table;
binsearch(table, hash, *size, &try);

// Calculate the position where the element would be inserted
Expand All @@ -217,7 +217,7 @@ bool lookup_insert(const enum memory_type type, const unsigned int id, const uin
// one position to the right to make space for the new element
// Don't move anything if the element is added at the end of the array
if(pos < *size)
memmove((void*)(try + 1), try, (*size - pos) * sizeof(struct lookup_table));
memmove(try + 1, try, (*size - pos) * sizeof(struct lookup_table));

// Prepare the new lookup_table element and insert it at the correct
// position
Expand Down Expand Up @@ -253,7 +253,7 @@ bool lookup_remove(const enum memory_type type, const unsigned int id, const uin
return false;

// Find the correct position in the lookup_table array
const struct lookup_table *try = NULL;
struct lookup_table *try = NULL;
if(!binsearch(table, hash, *size, &try))
{
// The element is not in the array
Expand Down Expand Up @@ -283,13 +283,14 @@ bool lookup_remove(const enum memory_type type, const unsigned int id, const uin
// one position to the left to remove the element
// Don't move anything if the element is removed from the end of the array
if(pos < *size - 1)
memmove(&table[pos], &table[pos + 1], (*size - pos - 1) * sizeof(struct lookup_table));
memmove(table + pos, table + pos + 1,
(*size - pos - 1) * sizeof(struct lookup_table));

// Decrease the number of elements in the array
(*size)--;

// Zero out the memory of the removed element
memset(&table[*size], 0, sizeof(struct lookup_table));
memset(table + *size, 0, sizeof(struct lookup_table));

return true;
}
Expand Down Expand Up @@ -332,7 +333,7 @@ bool lookup_find_id(const enum memory_type type, const uint32_t hash, const stru
return false;

// Find the correct position in the lookup_table array
const struct lookup_table *try = NULL;
struct lookup_table *try = NULL;
if(!binsearch(table, hash, *size, &try))
return false;

Expand Down
84 changes: 82 additions & 2 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ size_t _addstr(const char *input, const char *func, const int line, const char *
}

// Debugging output
log_debug(DEBUG_SHMEM, "Adding \"%s\" (len %zu) to buffer in %s() (%s:%i), next_str_pos is %u",
log_debug(DEBUG_SHMEM, "Adding \"%s\" (len %zu) to buffer in %s() (%s:%i), next_str_pos is %zu",
input, len, func, short_path(file), line, shmSettings->next_str_pos);

// Copy the C string pointed by input into the shared string buffer
Expand All @@ -266,7 +266,8 @@ const char *_getstr(const size_t pos, const char *func, const int line, const ch
return &((const char*)shm_strings.ptr)[pos];
else
{
log_warn("Tried to access %zu in %s() (%s:%i) but next_str_pos is %u", pos, func, file, line, shmSettings->next_str_pos);
log_warn("Tried to access %zu in %s() (%s:%i) but next_str_pos is %zu",
pos, func, file, line, shmSettings->next_str_pos);
return "";
}
}
Expand Down Expand Up @@ -1471,3 +1472,82 @@ void print_recycle_list_fullness(void)
log_info(" Domains: %u/%u (%.2f%%)", recycler->domain.count, RECYCLE_ARRAY_LEN, (double)recycler->domain.count / RECYCLE_ARRAY_LEN * 100.0);
log_info(" DNS Cache: %u/%u (%.2f%%)", recycler->dns_cache.count, RECYCLE_ARRAY_LEN, (double)recycler->dns_cache.count / RECYCLE_ARRAY_LEN * 100.0);
}

/**
* @brief Dumps the string table to a temporary file.
*
* This function iterates over the string table and writes each string to a temporary file
* located at "/tmp/stringdump.txt". It checks if each string is printable and escapes
* non-printable strings before writing them to the file. Additionally, it logs the number
* of non-printable strings and includes a human-readable timestamp in the output.
*
* The format of each line in the output file is:
* " " or "NONP" <string_index>: "<string_content>" (<current_position>/<string_length>)
*
* If the file cannot be opened for writing, an error message is logged.
*/
#define STRING_DUMPFILE "/tmp/stringdump.txt"
void dump_strings(void)
{
// Dump string table to temporary file
FILE *str_dumpfile = fopen(STRING_DUMPFILE, "a");
if(str_dumpfile != NULL)
{
char timestring[TIMESTR_SIZE] = { 0 };
get_timestr(timestring, time(NULL), true, false);
fprintf(str_dumpfile, "String dump starting at %s\n", timestring);
log_info("String dump to "STRING_DUMPFILE);

size_t j = 0, non_print = 0;
for(size_t i = 0; i < shmSettings->next_str_pos; i++)
{
char *sstr = (char*)getstr(i);
const size_t len = strlen(sstr);
char *buffer = sstr;
i += len;
j++;

// Check if the string is printable
bool string_is_printable = true;
for(size_t k = 0; k < len; k++)
{
if(!isprint(sstr[k]))
{
string_is_printable = false;
non_print++;
break;
}
}

// If the string is not printable, we escape it
if(!string_is_printable)
{
buffer = calloc(len * 4 + 1, sizeof(char));
if(buffer == NULL)
{
log_err("Failed to allocate memory for string buffer");
break;
}
binbuf_to_escaped_C_literal(sstr, len, buffer, len * 4 + 1);
}

// Print string to file
fprintf(str_dumpfile, "%s %04zu: \"%s\" (%zu/%zu)\n", string_is_printable ? " " : "NONP",
j, buffer, i, len);

// Free buffer if it was allocated
if(!string_is_printable)
free(buffer);
}

// Print human-readable timestamp and number of strings which are not printable
fprintf(str_dumpfile, "Summary: %zu strings\n", j);
fprintf(str_dumpfile, " %zu non-printable strings\n", non_print);
fprintf(str_dumpfile, "\n");

// Close file
fclose(str_dumpfile);
}
else
log_err("Cannot open "STRING_DUMPFILE" for writing: %s", strerror(errno));
}
4 changes: 3 additions & 1 deletion src/shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct {
int version;
pid_t pid;
unsigned int global_shm_counter;
unsigned int next_str_pos;
size_t next_str_pos;
unsigned int qps[QPS_AVGLEN];
} ShmSettings;

Expand Down Expand Up @@ -202,4 +202,6 @@ bool set_next_recycled_ID(const enum memory_type type, const unsigned int id);
bool get_next_recycled_ID(const enum memory_type type, unsigned int *id);
void print_recycle_list_fullness(void);

void dump_strings(void);

#endif //SHARED_MEMORY_SERVER_H

0 comments on commit 8634059

Please sign in to comment.