2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
18 #include <sys/socket.h>
27 #include "elliptics.h"
30 * Supported in Linux only so far
32 #ifdef HAVE_SENDFILE4_SUPPORT
33 #include <sys/prctl.h>
35 int dnet_set_name(char *n
)
40 int rest
= sizeof(name
) - sizeof(str
);
44 offset
= len
- rest
- 1;
46 snprintf(name
, sizeof(name
), "%s%s", str
, n
+ offset
);
47 return prctl(PR_SET_NAME
, name
);
50 #include <sys/syscall.h>
51 long dnet_get_id(void)
53 return syscall(SYS_gettid
);
56 int dnet_set_name(char *name
__attribute__ ((unused
))) { return 0; }
58 long dnet_get_id(void)
60 return pthread_self();
64 #ifdef HAVE_SENDFILE4_SUPPORT
65 #include <sys/sendfile.h>
66 int dnet_sendfile(struct dnet_net_state
*st
, int fd
, uint64_t *offset
, uint64_t size
)
70 err
= sendfile(st
->write_s
, fd
, (off_t
*)offset
, size
);
76 #elif HAVE_SENDFILE7_SUPPORT
78 int dnet_sendfile(struct dnet_net_state
*st
, int fd
, uint64_t *offset
, uint64_t size
)
82 err
= sendfile(fd
, st
->write_s
, *offset
, size
, NULL
, &size
, 0);
83 if (err
&& errno
!= EAGAIN
)
93 #elif HAVE_SENDFILE6_SUPPORT
95 int dnet_sendfile(struct dnet_net_state
*st
, int fd
, uint64_t *offset
, uint64_t size
)
99 err
= sendfile(fd
, st
->write_s
, *offset
, &size
, NULL
, 0);
100 if (err
&& errno
!= EAGAIN
)
111 int dnet_sendfile(struct dnet_net_state
*st
, int fd
, uint64_t *offset
, uint64_t size
)
117 err
= lseek(fd
, *offset
, SEEK_SET
);
120 dnet_log_err(st
->n
, "failed to seek to %llu",
121 (unsigned long long)*offset
);
128 if (sz
> sizeof(buf
))
131 err
= read(fd
, buf
, sz
);
133 if (errno
== EAGAIN
|| errno
== EINTR
)
136 dnet_log_err(st
->n
, "failed to read %zu bytes at %llu",
137 sz
, (unsigned long long)*offset
);
147 err
= send(st
->write_s
, buf
, sz
, 0);
149 if (errno
== EAGAIN
|| errno
== EINTR
)
174 #ifdef HAVE_IOPRIO_SUPPORT
184 IOPRIO_WHO_PROCESS
= 1,
190 * Gives us 8 prio classes with 13-bits of data for each class
192 #define IOPRIO_BITS (16)
193 #define IOPRIO_CLASS_SHIFT (13)
194 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
196 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
197 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
198 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
200 #define ioprio_valid(mask) (IOPRIO_PRIO_CLASS((mask)) != IOPRIO_CLASS_NONE)
202 int dnet_ioprio_set(long pid
, int class, int prio
)
204 return syscall(SYS_ioprio_set
, IOPRIO_WHO_PROCESS
, pid
, IOPRIO_PRIO_VALUE(class, prio
));
207 int dnet_ioprio_get(long pid
)
209 return syscall(SYS_ioprio_get
, IOPRIO_WHO_PROCESS
, pid
);
212 int dnet_ioprio_set(long pid
__attribute__ ((unused
)), int class __attribute__ ((unused
)), int prio
__attribute__ ((unused
))) { return 0; }
213 int dnet_ioprio_get(long pid
__attribute__ ((unused
))) { return 0; }