15 struct map_entry
*next
;
18 static struct hash_table mapper
;
19 static unsigned int *cpu_work_map
= NULL
;
20 static unsigned int cpu_len
= 0;
21 static struct rwlock map_lock
;
23 static unsigned int get_appropriate_cpu(void)
25 unsigned int i
, cpu
= 0;
26 unsigned int work
= UINT_MAX
;
28 for (i
= 0; i
< cpu_len
; ++i
) {
29 if (cpu_work_map
[i
] < work
) {
30 work
= cpu_work_map
[i
];
38 unsigned int socket_to_cpu(int fd
)
41 struct map_entry
*entry
;
43 rwlock_rd_lock(&map_lock
);
45 entry
= lookup_hash(fd
, &mapper
);
46 while (entry
&& fd
!= entry
->fd
)
49 if (entry
&& fd
== entry
->fd
)
52 rwlock_unlock(&map_lock
);
56 unsigned int register_socket(int fd
)
59 struct map_entry
*entry
;
61 rwlock_wr_lock(&map_lock
);
63 entry
= xzmalloc(sizeof(*entry
));
65 entry
->cpu
= get_appropriate_cpu();
67 cpu_work_map
[entry
->cpu
]++;
69 pos
= insert_hash(entry
->fd
, entry
, &mapper
);
75 rwlock_unlock(&map_lock
);
79 static struct map_entry
*socket_to_map_entry(int fd
)
81 struct map_entry
*entry
, *ret
= NULL
;
83 rwlock_rd_lock(&map_lock
);
85 entry
= lookup_hash(fd
, &mapper
);
86 while (entry
&& fd
!= entry
->fd
)
89 if (entry
&& fd
== entry
->fd
)
92 rwlock_unlock(&map_lock
);
96 void unregister_socket(int fd
)
98 struct map_entry
*pos
;
99 struct map_entry
*entry
= socket_to_map_entry(fd
);
104 rwlock_wr_lock(&map_lock
);
106 cpu_work_map
[entry
->cpu
]--;
108 pos
= remove_hash(entry
->fd
, entry
, entry
->next
, &mapper
);
109 while (pos
&& pos
->next
&& pos
->next
!= entry
)
112 if (pos
&& pos
->next
&& pos
->next
== entry
)
113 pos
->next
= entry
->next
;
118 rwlock_unlock(&map_lock
);
121 static int cleanup_cpusched_batch(void *ptr
)
123 struct map_entry
*next
;
124 struct map_entry
*entry
= ptr
;
129 while ((next
= entry
->next
)) {
140 void init_cpusched(unsigned int cpus
)
142 rwlock_init(&map_lock
);
144 cpu_work_map
= xcalloc(cpu_len
, sizeof(*cpu_work_map
));
148 void destroy_cpusched(void)
151 for_each_hash(&mapper
, cleanup_cpusched_batch
);
153 rwlock_destroy(&map_lock
);