Skip to content

Commit

Permalink
criu: optimize find_unix_sk_by_ino()
Browse files Browse the repository at this point in the history
Fixes: checkpoint-restore#339
Replaced the linear search with a hashtable lookup.

Signed-off-by: Zeyad Yasser <zeyady98@gmail.com>
  • Loading branch information
ZeyadYasser authored and Angie Ni committed Jun 12, 2020
1 parent d34e8a0 commit e687561
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions criu/files.c
Expand Up @@ -1749,5 +1749,6 @@ struct collect_image_info files_cinfo = {
int prepare_files(void)
{
init_fdesc_hash();
init_sk_info_hash();
return collect_image(&files_cinfo);
}
2 changes: 2 additions & 0 deletions criu/include/sockets.h
Expand Up @@ -62,6 +62,8 @@ extern int unix_sk_id_add(unsigned int ino);
extern int unix_sk_ids_parse(char *optarg);
extern int unix_prepare_root_shared(void);

extern void init_sk_info_hash(void);

extern int do_dump_opt(int sk, int level, int name, void *val, int len);
#define dump_opt(s, l, n, f) do_dump_opt(s, l, n, f, sizeof(*f))
extern int do_restore_opt(int sk, int level, int name, void *val, int len);
Expand Down
19 changes: 18 additions & 1 deletion criu/sk-unix.c
Expand Up @@ -903,6 +903,7 @@ struct unix_sk_info {
struct unix_sk_info *peer;
struct pprep_head peer_resolve; /* XXX : union with the above? */
struct file_desc d;
struct hlist_node hash; /* To lookup socket by ino */
struct list_head connected; /* List of sockets, connected to me */
struct list_head node; /* To link in peer's connected list */
struct list_head scm_fles;
Expand Down Expand Up @@ -934,11 +935,25 @@ struct scm_fle {
#define USK_PAIR_SLAVE 0x2
#define USK_GHOST_FDSTORE 0x4 /* bound but removed address */

#define SK_INFO_HASH_SIZE 32

static struct hlist_head sk_info_hash[SK_INFO_HASH_SIZE];

void init_sk_info_hash(void)
{
int i;

for (i = 0; i < SK_INFO_HASH_SIZE; i++)
INIT_HLIST_HEAD(&sk_info_hash[i]);
}

static struct unix_sk_info *find_unix_sk_by_ino(int ino)
{
struct unix_sk_info *ui;
struct hlist_head *chain;

list_for_each_entry(ui, &unix_sockets, list) {
chain = &sk_info_hash[ino % SK_INFO_HASH_SIZE];
hlist_for_each_entry(ui, chain, hash) {
if (ui->ue->ino == ino)
return ui;
}
Expand Down Expand Up @@ -2044,6 +2059,7 @@ static int init_unix_sk_info(struct unix_sk_info *ui, UnixSkEntry *ue)
INIT_LIST_HEAD(&ui->node);
INIT_LIST_HEAD(&ui->scm_fles);
INIT_LIST_HEAD(&ui->ghost_node);
INIT_HLIST_NODE(&ui->hash);

return 0;
}
Expand Down Expand Up @@ -2135,6 +2151,7 @@ static int collect_one_unixsk(void *o, ProtobufCMessage *base, struct cr_img *i)
list_add_tail(&ui->ghost_node, &unix_ghost_addr);
}

hlist_add_head(&ui->hash, &sk_info_hash[ui->ue->ino % SK_INFO_HASH_SIZE]);
list_add_tail(&ui->list, &unix_sockets);
return file_desc_add(&ui->d, ui->ue->id, &unix_desc_ops);
}
Expand Down

0 comments on commit e687561

Please sign in to comment.