summaryrefslogtreecommitdiffstats
path: root/build-root/copyimg
blob: e5e3fc265ee972de14e6124e6cf9183aac129f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh

if [ $# -lt 2 ]; then
    cat - <<EOF
$0 FROM-DIR TO-DIR ENVIRONMENT

Copies files from one directory to another with possible
transformations.

Files named FILE.spp will be transformed via the spp preprocessor
subject to environment definitions.  Source FILE.copyimgspp results in
destination file FILE in the corresponding destination directory.

Files named FILE.copyimgsh are run as shell scripts in (i.e. via chdir)
the corresponding destination directory (and not copied).

First regular files are copied.  Then transformations are preformed.
Finally, shell scripts are run.
EOF
  exit 1;
fi

FROM_DIR=$1
TO_DIR=$2

FILTER=" -and -not -name '*~'";
FILTER="${FILTER} -and -not -name '.*~'";
FILTER="$FILTER -and -not -path '*/.git*'";
FILTER="$FILTER -and -not -path '*/.svn*'";
FILTER="$FILTER -and -not -path '*/.CVS*'";

FROM_FILES=`(cd $FROM_DIR; eval "find . -not -type d $FILTER")`;
 FROM_DIRS=`(cd $FROM_DIR; eval "find .      -type d $FILTER")`;

COPY_FILES=
SPP_FILES=
SH_FILES=
for f in $FROM_FILES; do
  case $f in
    *.copyimgspp) SPP_FILES="$SPP_FILES $f" ;;
    *.copyimgsh)   SH_FILES="$SH_FILES $f" ;;
    *)		 COPY_FILES="$COPY_FILES $f";;
  esac
done

# Make destination directories.
mkdir -p $TO_DIR;
if [ "$FROM_DIRS" != "" ]; then
  for d in $FROM_DIRS; do
    mkdir -p $TO_DIR/$d;
  done
fi

# Copy files
if [ "$COPY_FILES" != "" ]; then
    tar -cf - -C $FROM_DIR $COPY_FILES | tar --preserve-permissions -xf - -C $TO_DIR;
fi

# Use spp to transform any spp files
if [ "$SPP_FILES" != "" ]; then
  for f in $SPP_FILES; do
    d=`dirname $f`;
    b=`basename $f .copyimgspp`;
    mkdir -p $TO_DIR/$d;
    t=$TO_DIR/$d/$b;
    spp -o $TO_DIR/$d/$b $FROM_DIR/$f || exit 1;
  done;
fi

# Now that all files have been copied/created we run any shell scripts
ABS_FROM_DIR=`(cd $FROM_DIR; pwd)`;
if [ "$SH_FILES" != "" ]; then
  # Allow directory to define some functions
  if [ -f $FROM_DIR/copyimgsh-functions.sh ]; then
    . $FROM_DIR/copyimgsh-functions.sh ;
  fi ;
  for f in $SH_FILES; do
    d=`dirname $f`;
    b=`basename $f`;
    mkdir -p $TO_DIR/$d;
    (cd $TO_DIR/$d; . $ABS_FROM_DIR/$d/$b) || exit 1;
  done;
fi;
m"> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <math.h> #include <stdlib.h> #include <string.h> #include <sys/param.h> #include <vppinfra/vec.h> #include <vppinfra/time.h> #include <vppinfra/timer.h> #include <vppinfra/error.h> typedef struct { f64 time; timer_func_t *func; any arg; } timer_callback_t; /* Vector of currently unexpired timers. */ static timer_callback_t *timers; /* Convert time from 64bit floating format to struct timeval. */ always_inline void f64_to_tv (f64 t, struct timeval *tv) { tv->tv_sec = t; tv->tv_usec = 1e6 * (t - tv->tv_sec); while (tv->tv_usec >= 1000000) { tv->tv_usec -= 1000000; tv->tv_sec += 1; } } /* Sort timers so that timer soonest to expire is at end. */ static int timer_compare (const void *_a, const void *_b) { const timer_callback_t *a = _a; const timer_callback_t *b = _b; f64 dt = b->time - a->time; return dt < 0 ? -1 : (dt > 0 ? +1 : 0); } static inline void sort_timers (timer_callback_t * timers) { qsort (timers, vec_len (timers), sizeof (timers[0]), timer_compare); } #define TIMER_SIGNAL SIGALRM /* Don't bother set timer if time different is less than this value. */ /* We would like to initialize this to 0.75 / (f64) HZ, * but HZ may not be a compile-time constant on some systems, * so instead we do the initialization before first use. */ static f64 time_resolution; /* Interrupt handler. Call functions for all expired timers. Set time for next timer interrupt. */ static void timer_interrupt (int signum) { f64 now = unix_time_now (); f64 dt; timer_callback_t *t; while (1) { if (vec_len (timers) <= 0) return; /* Consider last (earliest) timer in reverse sorted vector of pending timers. */ t = vec_end (timers) - 1; ASSERT (now >= 0 && finite (now)); /* Time difference between when timer goes off and now. */ dt = t->time - now; /* If timer is within threshold of going off call user's callback. */ if (dt <= time_resolution && finite (dt)) { _vec_len (timers) -= 1; (*t->func) (t->arg, -dt); } else { /* Set timer for to go off in future. */ struct itimerval itv; memset (&itv, 0, sizeof (itv)); f64_to_tv (dt, &itv.it_value); if (setitimer (ITIMER_REAL, &itv, 0) < 0) clib_unix_error ("sititmer"); return; } } } void timer_block (sigset_t * save) { sigset_t block_timer; memset (&block_timer, 0, sizeof (block_timer)); sigaddset (&block_timer, TIMER_SIGNAL); sigprocmask (SIG_BLOCK, &block_timer, save); } void timer_unblock (sigset_t * save) { sigprocmask (SIG_SETMASK, save, 0); } /* Arrange for function to be called some time, roughly equal to dt seconds, in the future. */ void timer_call (timer_func_t * func, any arg, f64 dt) { timer_callback_t *t; sigset_t save; /* Install signal handler on first call. */ static word signal_installed = 0; if (!signal_installed) { struct sigaction sa; /* Initialize time_resolution before first call to timer_interrupt */ time_resolution = 0.75 / (f64) HZ; memset (&sa, 0, sizeof (sa)); sa.sa_handler = timer_interrupt; if (sigaction (TIMER_SIGNAL, &sa, 0) < 0) clib_panic ("sigaction"); signal_installed = 1; } timer_block (&save); /* Add new timer. */ vec_add2 (timers, t, 1); t->time = unix_time_now () + dt; t->func = func; t->arg = arg; { word reset_timer = vec_len (timers) == 1; if (_vec_len (timers) > 1) { reset_timer += t->time < (t - 1)->time; sort_timers (timers); } if (reset_timer) timer_interrupt (TIMER_SIGNAL); } timer_unblock (&save); } #ifdef TEST #include <vppinfra/random.h> /* Compute average delay of function calls to foo. If this is a small number over a lot of iterations we know the code is working. */ static f64 ave_delay = 0; static word ave_delay_count = 0; always_inline update (f64 delay) { ave_delay += delay; ave_delay_count += 1; } typedef struct { f64 time_requested, time_called; } foo_t; static f64 foo_base_time = 0; static foo_t *foos = 0; void foo (any arg, f64 delay) { foos[arg].time_called = unix_time_now () - foo_base_time; update (delay); } typedef struct { word count; word limit; } bar_t; void bar (any arg, f64 delay) { bar_t *b = (bar_t *) arg; fformat (stdout, "bar %d delay %g\n", b->count++, delay); update (delay); if (b->count < b->limit) timer_call (bar, arg, random_f64 ()); } int main (int argc, char *argv[]) { word i, n = atoi (argv[1]); word run_foo = argc > 2; bar_t b = { limit:10 }; if (run_foo) { f64 time_limit; time_limit = atof (argv[2]); vec_resize (foos, n); for (i = 0; i < n; i++) { foos[i].time_requested = time_limit * random_f64 (); foos[i].time_called = 1e100; } foo_base_time = unix_time_now (); for (i = 0; i < n; i++) timer_call (foo, i, foos[i].time_requested); } else timer_call (bar, (any) & b, random_f64 ()); while (vec_len (timers) > 0) sched_yield (); if (vec_len (foos) > 0) { f64 min = 1e100, max = -min; f64 ave = 0, rms = 0; for (i = 0; i < n; i++) { f64 dt = foos[i].time_requested - foos[i].time_called; if (dt < min) min = dt; if (dt > max) max = dt; ave += dt; rms += dt * dt; } ave /= n; rms = sqrt (rms / n - ave * ave); fformat (stdout, "error min %g max %g ave %g +- %g\n", min, max, ave, rms); } fformat (stdout, "%d function calls, ave. timer delay %g secs\n", ave_delay_count, ave_delay / ave_delay_count); return 0; } #endif /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */