|
| 1 | +/* |
| 2 | + *parallel.c |
| 3 | + * |
| 4 | + *multi-process support |
| 5 | + * |
| 6 | + *Copyright (c) 2010-2012, PostgreSQL Global Development Group |
| 7 | + *contrib/pg_upgrade/parallel.c |
| 8 | + */ |
| 9 | + |
| 10 | +#include"postgres.h" |
| 11 | + |
| 12 | +#include"pg_upgrade.h" |
| 13 | + |
| 14 | +#include<stdlib.h> |
| 15 | +#include<string.h> |
| 16 | +#include<sys/types.h> |
| 17 | +#include<sys/wait.h> |
| 18 | + |
| 19 | +#ifdefWIN32 |
| 20 | +#include<io.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +staticintparallel_jobs; |
| 24 | + |
| 25 | +#ifdefWIN32 |
| 26 | +/* |
| 27 | + *Array holding all active threads. There can't be any gaps/zeros so |
| 28 | + *it can be passed to WaitForMultipleObjects(). We use two arrays |
| 29 | + *so the thread_handles array can be passed to WaitForMultipleObjects(). |
| 30 | + */ |
| 31 | +HANDLE*thread_handles; |
| 32 | + |
| 33 | +typedefstruct { |
| 34 | +charlog_file[MAXPGPATH]; |
| 35 | +charopt_log_file[MAXPGPATH]; |
| 36 | +charcmd[MAX_STRING]; |
| 37 | +}thread_arg; |
| 38 | + |
| 39 | +thread_arg**thread_args; |
| 40 | + |
| 41 | +DWORDwin32_exec_prog(thread_arg*args); |
| 42 | + |
| 43 | +#endif |
| 44 | + |
| 45 | +/* |
| 46 | + *parallel_exec_prog |
| 47 | + * |
| 48 | + *This has the same API as exec_prog, except it does parallel execution, |
| 49 | + *and therefore must throw errors and doesn't return an error status. |
| 50 | + */ |
| 51 | +void |
| 52 | +parallel_exec_prog(constchar*log_file,constchar*opt_log_file, |
| 53 | +constchar*fmt,...) |
| 54 | +{ |
| 55 | +va_listargs; |
| 56 | +charcmd[MAX_STRING]; |
| 57 | +#ifndefWIN32 |
| 58 | +pid_tchild; |
| 59 | +#else |
| 60 | +HANDLEchild; |
| 61 | +thread_arg*new_arg; |
| 62 | +#endif |
| 63 | + |
| 64 | +va_start(args,fmt); |
| 65 | +vsnprintf(cmd,sizeof(cmd),fmt,args); |
| 66 | +va_end(args); |
| 67 | + |
| 68 | +if (user_opts.jobs <=1) |
| 69 | +/* throw_error must be true to allow jobs */ |
| 70 | +exec_prog(log_file,opt_log_file, true,"%s",cmd); |
| 71 | +else |
| 72 | +{ |
| 73 | +/* parallel */ |
| 74 | + |
| 75 | +/* harvest any dead children */ |
| 76 | +while (reap_child(false)== true) |
| 77 | +; |
| 78 | + |
| 79 | +/* must we wait for a dead child? */ |
| 80 | +if (parallel_jobs >=user_opts.jobs) |
| 81 | +reap_child(true); |
| 82 | + |
| 83 | +/* set this before we start the job */ |
| 84 | +parallel_jobs++; |
| 85 | + |
| 86 | +/* Ensure stdio state is quiesced before forking */ |
| 87 | +fflush(NULL); |
| 88 | + |
| 89 | +#ifndefWIN32 |
| 90 | +child=fork(); |
| 91 | +if (child==0) |
| 92 | +/* use _exit to skip atexit() functions */ |
| 93 | +_exit(!exec_prog(log_file,opt_log_file, true,"%s",cmd)); |
| 94 | +elseif (child<0) |
| 95 | +/* fork failed */ |
| 96 | +pg_log(PG_FATAL,"could not create worker process: %s\n",strerror(errno)); |
| 97 | +#else |
| 98 | +if (thread_handles==NULL) |
| 99 | +{ |
| 100 | +inti; |
| 101 | + |
| 102 | +thread_handles=pg_malloc(user_opts.jobs*sizeof(HANDLE)); |
| 103 | +thread_args=pg_malloc(user_opts.jobs*sizeof(thread_arg*)); |
| 104 | + |
| 105 | +/* |
| 106 | + *For safety and performance, we keep the args allocated during |
| 107 | + *the entire life of the process, and we don't free the args |
| 108 | + *in a thread different from the one that allocated it. |
| 109 | + */ |
| 110 | +for (i=0;i<user_opts.jobs;i++) |
| 111 | +thread_args[i]=pg_malloc(sizeof(thread_arg)); |
| 112 | +} |
| 113 | + |
| 114 | +/* use first empty array element */ |
| 115 | +new_arg=thread_args[parallel_jobs-1]; |
| 116 | + |
| 117 | +/* Can only pass one pointer into the function, so use a struct */ |
| 118 | +strcpy(new_arg->log_file,log_file); |
| 119 | +strcpy(new_arg->opt_log_file,opt_log_file); |
| 120 | +strcpy(new_arg->cmd,cmd); |
| 121 | + |
| 122 | +child= (HANDLE)_beginthreadex(NULL,0, (void*)win32_exec_prog, |
| 123 | +new_arg,0,NULL); |
| 124 | +if (child==0) |
| 125 | +pg_log(PG_FATAL,"could not create worker thread: %s\n",strerror(errno)); |
| 126 | + |
| 127 | +thread_handles[parallel_jobs-1]=child; |
| 128 | +#endif |
| 129 | +} |
| 130 | + |
| 131 | +return; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +#ifdefWIN32 |
| 136 | +DWORD |
| 137 | +win32_exec_prog(thread_arg*args) |
| 138 | +{ |
| 139 | +intret; |
| 140 | + |
| 141 | +ret= !exec_prog(args->log_file,args->opt_log_file, true,"%s",args->cmd); |
| 142 | + |
| 143 | +/* terminates thread */ |
| 144 | +returnret; |
| 145 | +} |
| 146 | +#endif |
| 147 | + |
| 148 | + |
| 149 | +/* |
| 150 | + *collect status from a completed worker child |
| 151 | + */ |
| 152 | +bool |
| 153 | +reap_child(boolwait_for_child) |
| 154 | +{ |
| 155 | +#ifndefWIN32 |
| 156 | +intwork_status; |
| 157 | +intret; |
| 158 | +#else |
| 159 | +intthread_num; |
| 160 | +DWORDres; |
| 161 | +#endif |
| 162 | + |
| 163 | +if (user_opts.jobs <=1||parallel_jobs==0) |
| 164 | +return false; |
| 165 | + |
| 166 | +#ifndefWIN32 |
| 167 | +ret=waitpid(-1,&work_status,wait_for_child ?0 :WNOHANG); |
| 168 | + |
| 169 | +/* no children or, for WNOHANG, no dead children */ |
| 170 | +if (ret <=0|| !WIFEXITED(work_status)) |
| 171 | +return false; |
| 172 | + |
| 173 | +if (WEXITSTATUS(work_status)!=0) |
| 174 | +pg_log(PG_FATAL,"child worker exited abnormally: %s\n",strerror(errno)); |
| 175 | + |
| 176 | +#else |
| 177 | +/* wait for one to finish */ |
| 178 | +thread_num=WaitForMultipleObjects(parallel_jobs,thread_handles, |
| 179 | +false,wait_for_child ?INFINITE :0); |
| 180 | + |
| 181 | +if (thread_num==WAIT_TIMEOUT||thread_num==WAIT_FAILED) |
| 182 | +return false; |
| 183 | + |
| 184 | +/* compute thread index in active_threads */ |
| 185 | +thread_num-=WAIT_OBJECT_0; |
| 186 | + |
| 187 | +/* get the result */ |
| 188 | +GetExitCodeThread(thread_handles[thread_num],&res); |
| 189 | +if (res!=0) |
| 190 | +pg_log(PG_FATAL,"child worker exited abnormally: %s\n",strerror(errno)); |
| 191 | + |
| 192 | +/* dispose of handle to stop leaks */ |
| 193 | +CloseHandle(thread_handles[thread_num]); |
| 194 | + |
| 195 | +/*Move last slot into dead child's position */ |
| 196 | +if (thread_num!=parallel_jobs-1) |
| 197 | +{ |
| 198 | +thread_arg*tmp_args; |
| 199 | + |
| 200 | +thread_handles[thread_num]=thread_handles[parallel_jobs-1]; |
| 201 | + |
| 202 | +/* |
| 203 | + *We must swap the arg struct pointers because the thread we |
| 204 | + *just moved is active, and we must make sure it is not |
| 205 | + *reused by the next created thread. Instead, the new thread |
| 206 | + *will use the arg struct of the thread that just died. |
| 207 | + */ |
| 208 | +tmp_args=thread_args[thread_num]; |
| 209 | +thread_args[thread_num]=thread_args[parallel_jobs-1]; |
| 210 | +thread_args[parallel_jobs-1]=tmp_args; |
| 211 | +} |
| 212 | +#endif |
| 213 | + |
| 214 | +/* do this after job has been removed */ |
| 215 | +parallel_jobs--; |
| 216 | + |
| 217 | +return true; |
| 218 | +} |