Optimize some code.

This commit is contained in:
fengbojiang 2023-04-06 18:23:35 +08:00
parent fe7e360eca
commit edcac626c5
3 changed files with 50 additions and 13 deletions

View File

@ -9,7 +9,8 @@
#define SOCKET_OPS_ZONE_NAME "ff_socket_ops_zone_%d"
#define SOCKET_OPS_CONTEXT_MAX_NUM 32
/* Must be power of 2 */
#define SOCKET_OPS_CONTEXT_MAX_NUM (2 << 5)
#define SOCKET_OPS_CONTEXT_NAME_SIZE 32
#define SOCKET_OPS_CONTEXT_NAME "ff_so_context_"
@ -17,6 +18,12 @@
static uint16_t ff_max_so_context = SOCKET_OPS_CONTEXT_MAX_NUM;
struct ff_socket_ops_zone *ff_so_zone;
static inline int
is_power_of_2(uint64_t n)
{
return (n != 0 && ((n & (n - 1)) == 0));
}
int
ff_set_max_so_context(uint16_t count)
{
@ -30,6 +37,12 @@ ff_set_max_so_context(uint16_t count)
return 1;
}*/
if (!is_power_of_2(count)) {
ERR_LOG("Can not set: count:%d is not power of 2, use default:%d\n",
count, ff_max_so_context);
return -1;
}
ff_max_so_context = count;
return 0;
@ -66,7 +79,9 @@ ff_create_so_memzone()
rte_spinlock_init(&so_zone_tmp->lock);
so_zone_tmp->count = ff_max_so_context;
so_zone_tmp->mask = so_zone_tmp->count - 1;
so_zone_tmp->free = so_zone_tmp->count;
so_zone_tmp->idx = 0;
so_zone_tmp->sc = (struct ff_so_context *)(so_zone_tmp + 1);
for (i = 0; i < ff_max_so_context; i++) {
@ -134,18 +149,28 @@ ff_attach_so_context(int proc_id)
return NULL;
}
/* i从count-free开始尝试直到一轮结束 */
for (i = 0; i < ff_so_zone->count; i++) {
sc = &ff_so_zone->sc[i];
uint16_t idx = (ff_so_zone->idx + i) & ff_so_zone->mask;
sc = &ff_so_zone->sc[idx];
if (sc->inuse == 0) {
sc->inuse = 1;
rte_spinlock_init(&sc->lock);
sc->status = FF_SC_IDLE;
ff_so_zone->free--;
ff_so_zone->idx = idx;
break;
}
}
DEBUG_LOG("attach sc:%p, i:%d\n", sc, i);
if (unlikely(i == ff_so_zone->count)) {
ERR_LOG("Attach memzone failed: instance %d no free context,"
" fetel error of so status, all sc inuse, count:%d, free:%d\n",
proc_id, ff_so_zone->count, ff_so_zone->free);
sc = NULL;
}
DEBUG_LOG("attach sc:%p, so count:%d, free:%d, idx:%d, i:%d\n",
sc, ff_so_zone->count, ff_so_zone->free, ff_so_zone->idx, i);
rte_spinlock_unlock(&ff_so_zone->lock);

View File

@ -410,10 +410,6 @@ ff_handle_kevent(struct ff_so_context *sc)
static inline void
ff_handle_socket_ops(struct ff_so_context *sc)
{
if (sc->inuse == 0) {
return;
}
if (!rte_spinlock_trylock(&sc->lock)) {
return;
}
@ -446,14 +442,27 @@ ff_handle_socket_ops(struct ff_so_context *sc)
void
ff_handle_each_context()
{
uint16_t i;
uint16_t i, nb_handled;
rte_spinlock_lock(&ff_so_zone->lock);
for (i = 0; i < ff_so_zone->count; i++) {
struct ff_so_context *sc = &ff_so_zone->sc[i];
assert(ff_so_zone->count >= ff_so_zone->free);
nb_handled = ff_so_zone->count - ff_so_zone->free;
if (nb_handled) {
for (i = 0; i < ff_so_zone->count; i++) {
struct ff_so_context *sc = &ff_so_zone->sc[i];
ff_handle_socket_ops(sc);
if (sc->inuse == 0) {
continue;
}
ff_handle_socket_ops(sc);
nb_handled--;
if (!nb_handled) {
break;
}
}
}
rte_spinlock_unlock(&ff_so_zone->lock);

View File

@ -58,12 +58,15 @@ enum FF_SO_CONTEXT_STATUS {
struct ff_socket_ops_zone {
rte_spinlock_t lock;
/* total number of so_contex */
/* total number of so_contex, must be power of 2 */
uint16_t count;
uint16_t mask;
/* free number of so_context */
uint16_t free;
uint16_t idx;
struct ff_so_context *sc;
} __attribute__((packed));