Skip to content

Instantly share code, notes, and snippets.

@foamdino
Created April 24, 2025 07:38
Show Gist options
  • Select an option

  • Save foamdino/fcb717c5f30d02b16d0d7c5d3cdbdab0 to your computer and use it in GitHub Desktop.

Select an option

Save foamdino/fcb717c5f30d02b16d0d7c5d3cdbdab0 to your computer and use it in GitHub Desktop.
check IRQ affinity
/**
* Check the current irq affinity and core map to apply
*
* @param teb Pointer to our teb
* @param irq IRQ line number to check
* @param coremap Pointer to a coremap to apply
* @param is_equal Pointer to variabl that will receive coremap comparison result
*
* @return BL_OK | BL_ERR
*/
__BL_STATIC int
bl_pal_linux_check_irq_affinity(bl_teb_t *teb,
unsigned int irq,
bl_pal_coremap_t *coremap,
unsigned int *is_equal)
{
char irq_affinity_buf[PATH_MAX];
char *line = NULL;
FILE *file;
size_t size = 0;
ssize_t line_len;
int res, i;
bl_pal_coremap_t current_mask;
bl_assert(teb != NULL);
bl_assert(coremap != NULL);
bl_assert(is_equal != NULL);
bl_error_clear(teb);
res = bl_string_print_ex(irq_affinity_buf,
sizeof(irq_affinity_buf),
BL_STR_F_NOTRUNCATE,
NULL,
"/proc/irq/%u/smp_affinity",
irq);
if (res != BL_OK)
{
bl_error_set(teb, BL_ERR_STRING_TRUNCATED);
return BL_ERR;
}
file = fopen(irq_affinity_buf, "r");
if (!file)
{
bl_error_set_aux(teb,
BL_ERR_PAL_UNABLE_TO_OPEN_IRQ_AFFINITY,
errno,
"%s",
irq_affinity_buf);
return BL_ERR;
}
/* Initialise our current coremap */
bl_pal_clear_coremap(&current_mask);
/* Get the current irq affinity and instantiate the current_mask */
line_len = getline(&line, &size, file);
if (line_len > 0)
{
char *hex_words[bl_divide_round_up(BL_PAL_MAX_CORES, BL_BITS_PER_INT)];
unsigned int
coremap_words[bl_divide_round_up(BL_PAL_MAX_CORES, BL_BITS_PER_INT)];
/* Initialise our word values to zeroes */
memset(coremap_words, 0, sizeof(coremap_words));
/* Explode line into hexadecimal strings */
unsigned int num_words =
bl_string_str2arr(line, ',', hex_words, bl_arraysz(hex_words));
if (num_words > 0)
{
/* Loop over every word and convert the string to decimal value.
*
* Note the procfs orders the SMP affinity list from high order
* to low order, but the pal_coremap_set_words() API requires the
* word array to be in order from low order to high order.
*
* Thus we reorder the hex words being converted below, eg:
*
* 80000000,00000001 --> core96,core01
*
* And reversed into decimal words:
*
* coremap_words[0] = core01 (0x00000001)
* coremap_words[1] = core96 (0x80000000)
*/
for (i = num_words; i > 0; i--)
{
coremap_words[num_words - i] =
strtoul(hex_words[i - 1], NULL, 16);
}
/* Set words in current_mask */
res = bl_pal_set_coremap_by_u32word(teb,
&current_mask,
BL_PAL_COREMAP_ALL,
coremap_words,
num_words);
}
}
/* we use free instead of bl_free as getline contains an internal realloc which we
* do not track */
free(line);
fclose(file);
if (res != BL_OK)
return BL_ERR;
/* Check if the desired coremap is equal to the current_mask in the
* /proc/irq/%s/smp_affinity file */
*is_equal = bl_coremaps_equal(coremap, &current_mask, BL_PAL_MAX_CORES);
return BL_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment