Esempio n. 1
0
int ptask_create_prio(void (*task)(void), int period, int prio, int aflag) {
    tpars param = TASK_SPEC_DFL;
    param.period = tspec_from(period, MILLI);
    param.rdline = tspec_from(period, MILLI);
    param.priority = prio;
    param.act_flag = aflag;

    return __create_internal(task, &param);
}
Esempio n. 2
0
int ptask_create_edf(void (*task)(void), int period, int runtime, int dline,
                     int aflag) {
    tpars param = TASK_SPEC_DFL;
    param.period = tspec_from(period, MILLI);
    param.runtime = tspec_from(runtime, MILLI);
    param.rdline = tspec_from(dline, MILLI);
    param.act_flag = aflag;

    return __create_internal(task, &param);
}
Esempio n. 3
0
void gen() {
    int i, j;

    for (i = 1; i < num_tasks; i++) {
        tpars params = TASK_SPEC_DFL;
        params.period = tspec_from(period[i], MILLI);
        params.rdline = tspec_from(dline[i], MILLI);
        params.priority = prio[i];
        params.act_flag = DEFERRED;
        params.processor = 0;
        j = ptask_create_param(task, &params);
        if (j < 0)
            ptask_syserror("gen()", "error in creating task");
        ptask_activate_at(j, offset[i], MILLI);
    }
}
Esempio n. 4
0
int main() {
    ptask_init(SCHED_FIFO, GLOBAL, PRIO_INHERITANCE);

    tpars params = TASK_SPEC_DFL;
    params.priority = 2;
    params.act_flag = NOW;

    printf("Creating aperiodic task\n");
    aperiodic_id = ptask_create_param(myaperiodic, &params);

    if (aperiodic_id < 0) {
        printf("Cannot create aperiodic task\n");
        exit(-1);
    }

    params = TASK_SPEC_DFL;
    params.period = tspec_from(1, MILLI);
    params.priority = 1;
    params.act_flag = NOW;

    printf("Creating periodic task\n");
    int pid = ptask_create_param(myperiodic, &params);
    if (pid < 0) {
        printf("Cannot create aperiodic task\n");
        exit(-1);
    }

    pthread_join(ptask_get_threadid(pid), 0);
    pthread_join(ptask_get_threadid(aperiodic_id), 0);

    assert(ca == 5);

    return 0;
}
Esempio n. 5
0
/**
   d = a - b
 */
tspec
tspec_sub_delta (const tspec * a, ptime delta, int unit)
{
  tspec d;
  tspec b = tspec_from (delta, unit);
  d.tv_nsec = a->tv_nsec - b.tv_nsec;
  d.tv_sec = a->tv_sec - b.tv_sec;
  if (a->tv_nsec < b.tv_nsec)
    {
      d.tv_nsec += 1000000000;
      d.tv_sec -= 1;
    }
  return d;
}
Esempio n. 6
0
tspec
tspec_add_delta (const tspec * a, ptime delta, int unit)
{
  tspec d = tspec_from (delta, unit);
  tspec s;
  s.tv_nsec = a->tv_nsec + d.tv_nsec;
  s.tv_sec = a->tv_sec + d.tv_sec;
  while (s.tv_nsec >= 1000000000)
    {
      s.tv_nsec = s.tv_nsec - 1000000000;
      s.tv_sec += 1;
    }
  return s;
}
Esempio n. 7
0
void ptask_set_deadline(int i, int dline, int unit) {
    tspec new_dline;
    new_dline = tspec_from(dline, unit);

    pthread_mutex_lock(&_tp[i].mux);
    if (ptask_policy == SCHED_DEADLINE) {
        struct sched_attr *attr;
        attr = &_tp[i].schedattr;
        attr->sched_deadline = (__u64)tspec_to(&new_dline, NANO);
        if (sched_setattr(_tp[i].tid, attr, 0) != 0) {
            attr->sched_deadline = (__u64)tspec_to(&_tp[i].deadline, NANO);
            printf("ERROR in ptask_set_deadline !\n");
            perror("Error:");
            return;
        }
    }
    _tp[i].deadline = new_dline;
    pthread_mutex_unlock(&_tp[i].mux);
}
Esempio n. 8
0
void ptask_set_period(int i, int period, int unit) {
    tspec new_period;
    new_period = tspec_from(period, unit);

    pthread_mutex_lock(&_tp[i].mux);
    if (ptask_policy == SCHED_DEADLINE) {
        struct sched_attr *attr;
        attr = &_tp[i].schedattr;
        attr->sched_period = (__u64)tspec_to(&new_period, NANO);
        if (sched_setattr(_tp[i].tid, attr, 0) != 0) {
            attr->sched_period = (__u64)tspec_to(&_tp[i].period, NANO);
            printf("ERROR in ptask_set_period !\n");
            perror("Error:");
            return;
        }
    }
    _tp[i].period = new_period;
    pthread_mutex_unlock(&_tp[i].mux);
}
Esempio n. 9
0
void ptask_set_runtime(int i, int runtime, int unit) {
    tspec new_runtime;
    struct sched_attr *attr;

    if (ptask_policy != SCHED_DEADLINE) {
        printf("Error (ptask_set_runtime): The policy is not SCHED_DEADLINE\n");
        return;
    }

    new_runtime = tspec_from(runtime, unit);
    pthread_mutex_lock(&_tp[i].mux);
    attr = &_tp[i].schedattr;
    attr->sched_runtime = (__u64)tspec_to(&new_runtime, NANO);
    if (sched_setattr(_tp[i].tid, attr, 0) != 0) {
        attr->sched_runtime = (__u64)tspec_to(&_tp[i].runtime, NANO);
        printf("ERROR in ptask_set_runtime !\n");
        perror("Error:");
        return;
    }
    _tp[i].runtime = new_runtime;
    pthread_mutex_unlock(&_tp[i].mux);
}
Esempio n. 10
0
int ptask_activate_at(int i, ptime offset, int unit) {
    tspec reloff = tspec_from(offset, unit);
    tspec t;
    int ret = 1;

    pthread_mutex_lock(&_tp[i].mux);

    /* if (_tp[i].state == TASK_ACTIVE || _tp[i].state == TASK_WFP) { */
    if (_tp[i].state == TASK_WFP) {
        ret = -1;
    } else {
        t = tspec_get_ref();
        /* compute the absolute deadline */
        _tp[i].offset = tspec_add(&t, &reloff);
        _tp[i].dl = tspec_add(&_tp[i].offset, &_tp[i].deadline);
        /* compute the next activation time */
        _tp[i].at = tspec_add(&_tp[i].offset, &_tp[i].period);
        /* send the activation signal */
        sem_post(&_tsem[i]);
        // printf("sem_post done on task %d\n", i);
    }
    pthread_mutex_unlock(&_tp[i].mux);
    return ret;
}
Esempio n. 11
0
static int __create_internal(void (*task)(void), tpars *tp) {
    // pthread_attr_t	myatt;
    struct sched_param mypar;
    int tret;
    int j = 0;

    int i = allocate_tp();
    if (i == _TP_NOMORE)
        return -1;

    _tp[i].index = i;
    _tp[i].body = task;
    _tp[i].dmiss = 0;
    _tp[i].offset = tspec_zero;
    _tp[i].state = TASK_ACTIVE;
    _tp[i].cpu_id = -1;
    _tp[i].tid = -1;

    if (tp == NULL) {
        _tp[i].runtime = tspec_from(1, SEC);
        _tp[i].period = tspec_from(1, SEC);
        _tp[i].deadline = tspec_from(1, SEC);
        _tp[i].priority = (ptask_policy != SCHED_DEADLINE) ? 1 : 0;
        _tp[i].act_flag = DEFERRED;
        _tp[i].measure_flag = 0;
        _tp[i].arg = 0;
        _tp[i].modes = NULL;
    } else {
        _tp[i].runtime = tp->runtime;
        _tp[i].period = tp->period;
        _tp[i].deadline = tp->rdline;
        _tp[i].priority = tp->priority;
        _tp[i].act_flag = tp->act_flag;
        _tp[i].measure_flag = tp->measure_flag;
        _tp[i].arg = tp->arg;
        _tp[i].modes = tp->modes;
        if (tp->modes != NULL) {
            for (j = 0; j < tp->nmodes; ++j) {
                int result = rtmode_addtask(tp->modes, tp->mode_list[j], i);
                if (result == 0) {
                    release_tp(i);
                    return -1;
                }
            }
        }
    }

    pthread_attr_init(&_tp[i].attr);
    if (ptask_policy != SCHED_OTHER) {
        pthread_attr_setinheritsched(&_tp[i].attr, PTHREAD_EXPLICIT_SCHED);
    }

    if (ptask_policy != SCHED_DEADLINE) {
        pthread_attr_setschedpolicy(&_tp[i].attr, ptask_policy);
        mypar.sched_priority = _tp[i].priority;
        pthread_attr_setschedparam(&_tp[i].attr, &mypar);
    } else
        pthread_attr_setschedpolicy(&_tp[i].attr, SCHED_OTHER);
    cpu_set_t cpuset;
    if (ptask_global == PARTITIONED) {
        CPU_ZERO(&cpuset);
        CPU_SET(tp->processor, &cpuset);
        _tp[i].cpu_id = tp->processor;
        pthread_attr_setaffinity_np(&_tp[i].attr, sizeof(cpu_set_t), &cpuset);
    }

    tret = pthread_create(&_tid[i], &_tp[i].attr, ptask_std_body,
                          (void *)(&_tp[i]));
    pthread_attr_destroy(&_tp[i].attr);

    if (tret == 0) {
        return i;
    } else {
        release_tp(i);
        return -1;
    }
}
Esempio n. 12
0
File: ball.c Progetto: glipari/ptask
int main(void) {
    int c;                              /* character from keyboard	*/
    int i, j, k;                        /* number of tasks created	*/
    double a;                           /* temporary variable           */
    int h;                              /* temporary variable           */
    int ntasks = 0;                     /* total number of created tasks*/
    int last_proc = 0;                  /* last assigned processor      */
    int max_proc = ptask_getnumcores(); /* max number of procs  */

    init();

    a = 2. * G * (float)TOP;
    for (i = 0; i < MAX_TASKS; i++)
        v0[i] = sqrt(a);

    i = 0;
    do {
        k = 0;
        if (keypressed()) {
            c = readkey();
            k = c >> 8;
        }

        if ((ntasks == 0) && (k == KEY_SPACE)) {
            clear_to_color(screen, BGC);
            rect(screen, XMIN - L - 1, BASE - 1, XMAX + L + 1,
                 TOP + BASE + L + 1, 14);
        }

        if ((ntasks < MAX_TASKS) && (k == KEY_SPACE)) {
            tpars params = TASK_SPEC_DFL;
            params.period = tspec_from(PER, MILLI);
            params.rdline = tspec_from(DREL, MILLI);
            params.priority = PRIO - i;
            params.measure_flag = 1;
            params.act_flag = NOW;
            /* a round robin assignment */
            params.processor = last_proc++;
            if (last_proc >= max_proc)
                last_proc = 0;

            /** i = task_create(palla, PER, DREL, PRIO-i, NOW); */
            i = ptask_create_param(palla, &params);
            if (i != -1) {
                printf("Task %d created and activated\n", i);
                ntasks++;
            } else {
                allegro_exit();
                printf("Error in creating task!\n");
                exit(-1);
            }
        }

        if ((k >= KEY_0) && (k <= KEY_9)) {
            a = 2. * G * (float)TOP;
            pthread_mutex_lock(&mxv);
            v0[k - KEY_0] = sqrt(a);
            pthread_mutex_unlock(&mxv);
        }

        if ((k == KEY_O) && (ntasks > 9)) {
            for (j = 10; j < ntasks; j++) {
                h = rand() % (TOP - BASE);
                a = 2. * G * (float)h;
                pthread_mutex_lock(&mxv);
                v0[j] = sqrt(a);
                pthread_mutex_unlock(&mxv);
            }
        }

        if (k == KEY_A) {
            for (j = 0; j < ntasks; j++) {
                h = rand() % (TOP - BASE);
                a = 2. * G * (float)h;
                pthread_mutex_lock(&mxv);
                v0[j] = sqrt(a);
                pthread_mutex_unlock(&mxv);
            }
        }

        /*
          Printing deadline misses
          TO BE DONE
          for (j=0; j<ntasks; j++) {
          sprintf(s, "%d", task_dmiss(j));
          textout_ex(screen, font, s, 50+j*48, 450, 7, 0);
          }
        */

    } while (k != KEY_ESC);

    printf("Now printing the stats\n");
    for (j = 0; j < ntasks; j++) {
        tspec wcet = ptask_get_wcet(j);
        tspec acet = ptask_get_avg(j);

        printf("TASK %d: WCET = %ld\t ACET = %ld\t NINST=%d\n", j,
               tspec_to(&wcet, MICRO), tspec_to(&acet, MICRO),
               ptask_get_numinstances(j));
    }

    printf("End of statistics\n");
    allegro_exit();
    return 0;
}
Esempio n. 13
0
int main(void) {
    int i, j;
    int aper, act_aper;

    init();

    tpars params = TASK_SPEC_DFL;
    params.period = tspec_from(PER, MILLI);
    params.rdline = tspec_from(DREL, MILLI);
    params.measure_flag = 1;
    params.processor = 0;
    params.act_flag = DEFERRED;
    params.runtime = tspec_from(RNT, MILLI);
    params.priority = PRIO;
    
    i = ptask_create_param(task, &params);
    if (i != -1) printf("Task %d created and activated\n", i);
    else exit(-1);
    ptask_activate_at(i, 50, MILLI);
    
    params.priority = PRIO - i;
    j = ptask_create_param(task, &params);
    if (j != -1) printf("Task %d created and activated\n", j);
    else exit(-1);
    ptask_activate_at(j, 50, MILLI);
    
    /* params.priority = PRIO - j; */
    /* k = ptask_create_param(task, &params); */
    /* if (k != -1) printf("Task %d created and activated\n", k); */
    /* else exit(-1); */
    /* ptask_activate_at(k, 50, MILLI); */
    
    /* params.priority = PRIO - k; */
    /* l = ptask_create_param(task, &params); */
    /* if (l != -1) printf("Task %d created and activated\n", l); */
    /* else exit(-1); */
    /* ptask_activate_at(l, 50, MILLI); */

    /* params.priority = PRIO - l; */
    /* m = ptask_create_param(task, &params); */
    /* if (m != -1) printf("Task %d created and activated\n", m); */
    /* else exit(-1); */
    /* ptask_activate_at(m, 50, MILLI); */

    /* params.priority = PRIO - m; */
    /* n = ptask_create_param(task, &params); */
    /* if (n != -1) printf("Task %d created and activated\n", n); */
    /* else exit(-1); */
    /* ptask_activate_at(n, 50, MILLI); */

    aper = ptask_create_param(aper_task, &params);
    if (aper != -1) printf("Task %d created and activated\n", aper);
    else exit(-1);

    params.arg = &aper;
    act_aper = ptask_create_param(activ_aper_task, &params);
    if (act_aper != -1) printf("Task %d created and activated\n", act_aper);
    else exit(-1);
    ptask_activate_at(act_aper, 50, MILLI);
    
    while(1) ;
    
    return 0;
}