1 // -*- c-basic-offset: 4; related-file-name: "../../include/click/standard/scheduleinfo.hh" -*-
2 /*
3  * scheduleinfo.{cc,hh} -- element stores schedule parameters
4  * Benjie Chen, Eddie Kohler
5  *
6  * Copyright (c) 1999-2000 Massachusetts Institute of Technology
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, subject to the conditions
11  * listed in the Click LICENSE file. These conditions include: you must
12  * preserve this copyright notice, and you cannot mention the copyright
13  * holders in advertising related to the Software without their permission.
14  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
15  * notice is a summary of the Click LICENSE file; the license in that file is
16  * legally binding.
17  */
18 
19 #include <click/config.h>
20 #include <click/standard/scheduleinfo.hh>
21 #include <click/nameinfo.hh>
22 #include <click/glue.hh>
23 #include <click/confparse.hh>
24 #include <click/router.hh>
25 #include <click/error.hh>
26 #include <click/nameinfo.hh>
27 CLICK_DECLS
28 
ScheduleInfo()29 ScheduleInfo::ScheduleInfo()
30 {
31 #if HAVE_STRIDE_SCHED
32     static_assert((1 << FRAC_BITS) == Task::DEFAULT_TICKETS, "Stride scheduler constant issue.");
33 #endif
34 }
35 
36 int
configure(Vector<String> & conf,ErrorHandler * errh)37 ScheduleInfo::configure(Vector<String> &conf, ErrorHandler *errh)
38 {
39     NameDB* db = NameInfo::getdb(NameInfo::T_SCHEDULEINFO, this, 4, true);
40 
41     // compile scheduling info
42     for (int i = 0; i < conf.size(); i++) {
43 	Vector<String> parts;
44 	uint32_t mt;
45 	cp_spacevec(conf[i], parts);
46 	if (parts.size() == 0)
47 	    /* empty argument OK */;
48 	else if (parts.size() != 2 || !FixedPointArg(FRAC_BITS).parse(parts[1], mt))
49 	    errh->error("expected %<ELEMENTNAME PARAM%>");
50 	else
51 	    db->define(parts[0], &mt, 4);
52     }
53 
54     return 0;
55 }
56 
57 int
query(Element * e,ErrorHandler * errh)58 ScheduleInfo::query(Element *e, ErrorHandler *errh)
59 {
60 #if HAVE_STRIDE_SCHED
61     // check prefixes in order of increasing length
62     String id = e->name();
63 
64     Vector<String> prefixes;
65     prefixes.push_back(String());
66     const char *slash = id.begin();
67     while ((slash = find(slash, id.end(), '/')) < id.end()) {
68 	prefixes.push_back(id.substring(id.begin(), slash + 1));
69 	slash++;
70     }
71     prefixes.push_back(id);
72 
73     Vector<uint32_t> tickets(prefixes.size(), Task::DEFAULT_TICKETS);
74 
75     NameDB *db = NameInfo::getdb(NameInfo::T_SCHEDULEINFO, e, 4, false);
76     while (db) {
77 	bool frobbed = false;
78 	for (int i = prefixes.size() - 1;
79 	     i >= 0 && prefixes[i].length() >= db->context().length();
80 	     i--)
81 	    if (db->query(prefixes[i].substring(db->context().length()), &tickets[i], 4))
82 		frobbed = true;
83 	    else if (frobbed)	// erase intermediate ticket settings
84 		tickets[i] = Task::DEFAULT_TICKETS;
85 	db = db->context_parent();
86     }
87 
88     // multiply tickets
89     int tickets_out = tickets[0];
90     for (int i = 1; i < tickets.size(); i++)
91 	if (tickets[i] != Task::DEFAULT_TICKETS)
92 #ifdef HAVE_INT64_TYPES
93 	    tickets_out = ((int64_t) tickets_out * tickets[i]) >> FRAC_BITS;
94 #else
95 	    tickets_out = (tickets_out * tickets[i]) >> FRAC_BITS;
96 #endif
97 
98     // check for too many tickets
99     if (tickets_out > Task::MAX_TICKETS) {
100 	tickets_out = Task::MAX_TICKETS;
101 	String m = cp_unparse_real2(tickets_out, FRAC_BITS);
102 	errh->warning("ScheduleInfo too high; reduced to %s", m.c_str());
103     }
104 
105     // return the result you've got
106     return tickets_out;
107 #else
108     (void) e, (void) errh;
109     return 1;
110 #endif
111 }
112 
113 void
initialize_task(Element * e,Task * task,bool schedule,ErrorHandler * errh)114 ScheduleInfo::initialize_task(Element *e, Task *task, bool schedule,
115 			      ErrorHandler *errh)
116 {
117 #if HAVE_STRIDE_SCHED
118     int tickets = query(e, errh);
119     if (tickets > 0) {
120 	task->initialize(e, schedule);
121 	task->set_tickets(tickets);
122     }
123 #else
124     (void) errh;
125     task->initialize(e, schedule);
126 #endif
127 }
128 
129 CLICK_ENDDECLS
130 EXPORT_ELEMENT(ScheduleInfo)
131 ELEMENT_HEADER(<click/standard/scheduleinfo.hh>)
132