+#include <frsh.h>
+#include <error.h>
+#include <signal.h>
+#include <getopt.h>
+#include <ul_logreg.h>
+#include <res_dummy.h>
+
+volatile int finish = 0;
+
+void int_handler(int signal)
+{
+ finish = 1;
+}
+
+static struct option long_opts[] = {
+ { "loglevel", 1, 0, 'l' },
+ { 0, 0, 0, 0}
+};
+
+static void
+usage(void)
+{
+ printf("usage: cpu_renegotiation [ options ]\n");
+ printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
+}
+
+
+#define MSEC(x) { x/1000, (x%1000) * 1000000 }
+frsh_utilization_set_t utilization_set = {
+ .size = 3,
+ .utilizations = {
+ { .budget = MSEC(10), .period = MSEC(100), .deadline = MSEC(100) },
+ { .budget = MSEC(20), .period = MSEC(100), .deadline = MSEC(100) },
+ { .budget = MSEC(30), .period = MSEC(100), .deadline = MSEC(100) },
+ },
+};
+
+int main(int argc, char *argv[])
+{
+ frsh_vres_id_t vres;
+ frsh_contract_t contract;
+ int ret;
+ char opt;
+
+ while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
+ switch (opt) {
+ case 'l':
+ ul_log_domain_arg2levels(optarg);
+ break;
+ case 'h':
+ /*default:*/
+ usage();
+ exit(opt == 'h' ? 0 : 1);
+ }
+ }
+
+ ret = frsh_init();
+ if (ret) PERROR_AND_EXIT(ret, "frsh_init");
+
+ signal(SIGINT, int_handler);
+ signal(SIGTERM, int_handler);
+
+ /* Contract negotiation for CPU */
+ ret = frsh_contract_init(&contract);
+ if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
+
+ int variant = 0;
+ bool first = true;
+ while (!finish) {
+
+ ret = frsh_contract_set_basic_params(&contract,
+ &utilization_set.utilizations[variant].budget,
+ &utilization_set.utilizations[variant].period,
+ FRSH_WT_BOUNDED,
+ FRSH_CT_REGULAR);
+ if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
+
+ ret = frsh_contract_set_resource_and_label(&contract,
+ DUMMY_RESOURCE_TYPE, DUMMY_RESOURCE_ID,
+ "renegotiation test");
+ if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
+
+ if (first) {
+ ret = frsh_contract_negotiate(&contract, &vres);
+ if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
+ first = false;
+ } else {
+ ret = frsh_contract_renegotiate_sync(&contract, vres);
+ if (ret == 0) {
+ printf("Renegotiation accepted\n");
+ } else if (ret == FRSH_ERR_CONTRACT_REJECTED) {
+ printf("Renegotiaton REJECTED\n");
+ } else
+ PERROR_AND_EXIT(ret, "frsh_contract_renegotiate_sync");
+ }
+ sleep(5);
+ variant++;
+ if (variant >= utilization_set.size) variant = 0;
+ }
+
+ ret = frsh_contract_cancel(vres);
+ if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
+
+ return 0;
+}
+