aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-10-04 19:09:20 +0200
committerJ08nY2017-10-04 19:09:20 +0200
commitf708453b55b6b05187febf3d5a2bd8608a897370 (patch)
tree340d36ad38ae355aa606820e5a1c045df977cbec
parentcaa000e3625241b930fdcda1594bbaf9c9acf642 (diff)
downloadecgen-f708453b55b6b05187febf3d5a2bd8608a897370.tar.gz
ecgen-f708453b55b6b05187febf3d5a2bd8608a897370.tar.zst
ecgen-f708453b55b6b05187febf3d5a2bd8608a897370.zip
-rw-r--r--src/exhaustive/exhaustive.c11
-rw-r--r--src/io/cli.c21
-rw-r--r--src/misc/config.h1
-rw-r--r--src/util/timeout.c2
-rw-r--r--src/util/timeout.h20
5 files changed, 46 insertions, 9 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 30d2c47..5fa2082 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -15,6 +15,7 @@
#include "gen/seed.h"
#include "io/output.h"
#include "util/memory.h"
+#include "util/timeout.h"
exhaustive_t *exhaustive_new(void) { return try_calloc(sizeof(exhaustive_t)); }
@@ -230,7 +231,15 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
arg_t *arg = argss ? argss[state] : NULL;
- int diff = generators[state](curve, cfg, arg, (offset_e)state);
+ int diff;
+ timeout_start(cfg->timeout) {
+ // This is not the best, but currently the best idea I have.
+ diff = start_offset - state;
+ }
+ else {
+ diff = generators[state](curve, cfg, arg, (offset_e)state);
+ }
+ timeout_stop();
int new_state = state + diff;
if (new_state < start_offset) new_state = start_offset;
diff --git a/src/io/cli.c b/src/io/cli.c
index 77f279f..74e6cc7 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -4,7 +4,6 @@
*/
#include "cli.h"
#include <string.h>
-#include <unistd.h>
#include "exhaustive/ansi.h"
char cli_doc[] =
@@ -34,6 +33,7 @@ enum opt_keys {
OPT_POINTS,
OPT_THREADS,
OPT_TSTACK,
+ OPT_TIMEOUT,
OPT_ANOMALOUS
};
@@ -68,6 +68,7 @@ struct argp_option cli_options[] = {
{"memory", OPT_MEMORY, "SIZE", 0, "Use PARI stack of SIZE (can have suffix k/m/g).", 4},
{"threads", OPT_THREADS, "NUM", 0, "Use NUM threads.", 4},
{"thread-stack", OPT_TSTACK, "SIZE", 0, "Use PARI stack of SIZE (per thread, can have suffix k/m/g).", 4},
+ {"timeout", OPT_TIMEOUT, "TIME", 0, "Timeout computation of a curve parameter after TIME (can have suffix s/m/h/d).", 4},
{0}
};
// clang-format on
@@ -87,6 +88,21 @@ static unsigned long cli_parse_memory(const char *str) {
return read;
}
+static unsigned long cli_parse_time(const char *str) {
+ char *suffix = NULL;
+ unsigned long read = strtoul(str, &suffix, 10);
+ if (suffix) {
+ if (*suffix == 'm' || *suffix == 'M') {
+ read *= 60;
+ } else if (*suffix == 'h' || *suffix == 'H') {
+ read *= 3600;
+ } else if (*suffix == 'd' || *suffix == 'D') {
+ read *= 86400;
+ }
+ }
+ return read;
+}
+
error_t cli_parse(int key, char *arg, struct argp_state *state) {
config_t *cfg = state->input;
@@ -100,6 +116,9 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
case OPT_TSTACK:
cfg->thread_memory = cli_parse_memory(arg);
break;
+ case OPT_TIMEOUT:
+ cfg->timeout = cli_parse_time(arg);
+ break;
case OPT_THREADS:
if (!strcmp(arg, "auto") || !strcmp(arg, "AUTO")) {
long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
diff --git a/src/misc/config.h b/src/misc/config.h
index 5069881..fe792d5 100644
--- a/src/misc/config.h
+++ b/src/misc/config.h
@@ -58,6 +58,7 @@ typedef struct {
unsigned long memory;
unsigned long threads;
unsigned long thread_memory;
+ unsigned long timeout;
enum format_e format;
char *output;
diff --git a/src/util/timeout.c b/src/util/timeout.c
index c9e20e0..c9608a9 100644
--- a/src/util/timeout.c
+++ b/src/util/timeout.c
@@ -5,7 +5,7 @@
#include "timeout.h"
__thread jmp_buf timeout_ptr;
-__thread bool timeout_in;
+__thread bool timeout_in = false;
__thread timer_t timeout_timer;
void timeout_handle(int signum, siginfo_t *siginfo, void *other) {
diff --git a/src/util/timeout.h b/src/util/timeout.h
index 0226fda..2b615bb 100644
--- a/src/util/timeout.h
+++ b/src/util/timeout.h
@@ -16,8 +16,11 @@ extern __thread sigjmp_buf timeout_ptr;
extern __thread bool timeout_in;
extern __thread timer_t timeout_timer;
+/**
+ * @brief
+ */
#define timeout_start(seconds) \
- { \
+ if ((seconds) != 0) { \
struct sigevent sevp; \
sevp.sigev_notify = SIGEV_THREAD_ID; \
sevp.sigev_signo = SIGALRM; \
@@ -30,12 +33,17 @@ extern __thread timer_t timeout_timer;
timer_settime(timeout_timer, 0, &timer_time, NULL); \
timeout_in = true; \
}; \
- if (sigsetjmp(timeout_ptr, 1) == 1)
+ if ((seconds) != 0 && sigsetjmp(timeout_ptr, 1) == 1)
-#define timeout_stop() \
- { \
- timeout_in = false; \
- timer_delete(timeout_timer); \
+/**
+ * @brief
+ */
+#define timeout_stop() \
+ { \
+ if (timeout_in) { \
+ timeout_in = false; \
+ timer_delete(timeout_timer); \
+ } \
}
/**