Debian Codegraph @

hmmer/src/hmmpgmd_shard.c

raw symbols SCIP
1 /* HMMER search daemon
2 */
3 #include <p7_config.h>
4
5 #ifdef HMMER_THREADS
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11
12 #ifndef HMMER_THREADS
13 #error "Program requires pthreads be enabled."
14 #endif /*HMMER_THREADS*/
15
16 #include "easel.h"
17 #include "esl_getopts.h"
18
19 #include "hmmer.h"
20 #include "hmmpgmd.h"
21 #include "hmmpgmd_shard.h"
22
23 #define CONF_FILE "/etc/hmmpgmd.conf"
24
25 static ESL_OPTIONS cmdlineOpts[] = {
26 /* name type default env range toggles reqs incomp help docgroup */
27 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
28 { "--master", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--worker", "run program as the master server", 12 },
29 { "--worker", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, "--master", "run program as a worker with server at <s>", 12 },
30 { "--cport", eslARG_INT, "51371", NULL, "49151<n<65536",NULL, NULL, "--worker", "port to use for client/server communication", 12 },
31 { "--wport", eslARG_INT, "51372", NULL, "49151<n<65536",NULL, NULL, NULL, "port to use for server/worker communication", 12 },
32 { "--ccncts", eslARG_INT, "16", NULL, "n>0", NULL, NULL, "--worker", "maximum number of client side connections to accept", 12 },
33 { "--wcncts", eslARG_INT, "32", NULL, "n>0", NULL, NULL, "--worker", "maximum number of worker side connections to accept", 12 },
34 { "--pid", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "file to write process id to", 12 },
35 { "--seqdb", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--worker", "protein database to cache for searches", 12 },
36 { "--hmmdb", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--worker", "hmm database to cache for searches", 12 },
37 { "--cpu", eslARG_INT, p7_NCPU,"HMMER_NCPU","n>0", NULL, NULL, "--master", "number of parallel CPU workers to use for multithreads", 12 },
38 { "--num_shards", eslARG_INT, "1", NULL, "1<=n<512", NULL, NULL, "--worker", "number of worker nodes that will connect to the master", 12 },
39 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
40 };
41
42 static char usage[] = "[options]";
43 static char banner[] = "search a query against a database";
44
45
46 typedef void sig_func(int);
47
48 /* MSF called this <signal()>. That conflicts with system's <signal()>.
49 * Unclear what he was doing. This appears to duplicated functionality
50 * of the system's <signal()>. Revisit later. For now, silence compiler
51 * warning by renaming the function.
52 */
53 sig_func *
54 our_signal(int signo, sig_func *fn)
55 {
56 struct sigaction act;
57 struct sigaction oact;
58
59 act.sa_handler = fn;
60 sigemptyset(&act.sa_mask);
61 act.sa_flags = 0;
62 if (signo == SIGALRM) {
63 #ifdef SA_INTERRUPT
64 act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
65 #endif
66 } else {
67 #ifdef SA_RESTART
68 act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
69 #endif
70 }
71 if (sigaction(signo, &act, &oact) < 0) {
72 return SIG_ERR;
73 }
74
75 return oact.sa_handler;
76 }
77
78 /* write_pid()
79 * Log the process id to a file.
80 */
81 static void
82 write_pid(ESL_GETOPTS *go)
83 {
84 char *pid_file = esl_opt_GetString(go, "--pid");
85 FILE *fp = fopen(pid_file, "w");
86
87 if (!fp) p7_Fail("Unable to open PID file %s for writing.", pid_file);
88 fprintf(fp,"%ld\n", (long)getpid());
89 fclose(fp);
90 }
91
92 static int
93 process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go)
94 {
95 ESL_GETOPTS *go = esl_getopts_Create(cmdlineOpts);
96 int status;
97
98 /* if there are no command line arguments, let's try to read /etc/hmmpgmd.conf
99 * for any configuration data.
100 */
101 if (argc == 1)
102 {
103 FILE *fp = NULL;
104
105 if ((fp = fopen(CONF_FILE, "r")) == NULL)
106 { if (puts("Options --master or --worker must be specified.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
107
108 if ((status = esl_opt_ProcessConfigfile(go, CONF_FILE, fp) ) != eslOK)
109 { if (printf("Failed to parse configuration file %s: %s\n", CONF_FILE, go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
110 fclose(fp);
111 }
112 else
113 {
114 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK)
115 { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
116 }
117
118 if (esl_opt_VerifyConfig(go) != eslOK) { if (printf("Failed to parse command line: %s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
119
120 /* help format: */
121 if (esl_opt_GetBoolean(go, "-h") == TRUE)
122 {
123 p7_banner(stdout, argv[0], banner);
124 esl_usage(stdout, argv[0], usage);
125
126 if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
127 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
128
129 if (puts("\nOther expert options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
130 esl_opt_DisplayHelp(stdout, go, 12, 2, 80);
131 exit(0);
132 }
133
134
135 if (esl_opt_ArgNumber(go) != 0) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
136
137 if (esl_opt_IsUsed(go, "--master") && !(esl_opt_IsUsed(go, "--seqdb") || esl_opt_IsUsed(go, "--hmmdb")))
138 { if (puts("At least one --seqdb or --hmmdb must be specified.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
139
140 *ret_go = go;
141 return eslOK;
142
143 FAILURE: /* all errors handled here are user errors, so be polite. */
144 esl_usage(stdout, argv[0], usage);
145 puts("\nwhere most common options are:");
146 esl_opt_DisplayHelp(stdout, go, 1, 2, 80); /* 1= group; 2 = indentation; 80=textwidth*/
147 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
148 esl_getopts_Destroy(go);
149 exit(0);
150
151 ERROR:
152 if (go) esl_getopts_Destroy(go);
153 exit(status);
154 }
155
156 int
157 main(int argc, char **argv)
158 {
159 ESL_GETOPTS *go = NULL; /* command line processing */
160
161 process_commandline(argc, argv, &go);
162
163 /* if we write to a broken socket, ignore the signal and handle the error. */
164 our_signal(SIGPIPE, SIG_IGN);
165
166 /* check if we need to write out our pid */
167 if (esl_opt_IsOn(go, "--pid")) write_pid(go);
168
169 if (esl_opt_IsUsed(go, "--master")) master_process_shard(go);
170 else if (esl_opt_IsUsed(go, "--worker")) worker_process_shard(go);
171 else
172 { puts("Options --master or --worker must be specified."); }
173
174 esl_getopts_Destroy(go);
175
176 return eslOK;
177 }
178
179
180 /*****************************************************************
181 * Notes on testing and debugging
182 *****************************************************************/
183
184
185 /* How to run a test:
186 * Need a test sequence database: mine is ~eddys/data/hmmpgmd/hmmpgmd.fa
187 * Need a test profile database: mine is ~eddys/data/hmmpgmd/hmmpgmd.hmm
188 *
189 * src/hmmpgmd --master --seqdb ~/data/hmmpgmd/hmmpgmd.fa --hmmdb ~/data/hmmpgmd/hmmpgmd.hmm &
190 * src/hmmpgmd --worker 127.0.0.1 --cpu 1 &
191 * src/hmmc2 -S
192 * @--seqdb 1
193 * >foo
194 * ACDEFGH...
195 * //
196 * !shutdown
197 * //
198 *
199 * Or, for hmmscan against the hmm db, replace @--seqdb 1 with @--hmmdb 1.
200 *
201 * For debugging, start two of the three processes on cmdline, and the
202 * one to be debugged under gdb.
203 * in worker: break process_SearchCmd
204 *
205 *
206 * Valgrind debugging
207 * generate a small test seq database:
208 * esl-shuffle -G --amino -N 10 -L 400 > foo.fa
209 * fasta2daemon.pl foo.fa > foo.d
210 *
211 * in three separate terminal windows
212 * valgrind --dsymutil=yes --leak-check=yes --show-reachable=yes src/hmmpgmd --master --seqdb foo.d
213 * valgrind --leak-check=yes --show-reachable=yes src/hmmpgmd --worker 127.0.0.1 --cpu 1
214 * valgrind --dsymutil=yes --leak-check=yes --show-reachable=yes src/hmmc2 -S
215 *
216 * copy/paste tutorial/M1.hmm as an example hmmsearch
217 * or any HMM. On Mac OS/X, to "copy" a file my.hmm to the clipboard:
218 * cat my.hmm | pbcopy
219 */
220
221 #endif /*HMMER_THREADS*/
222