12fc2a97a1dfd2897d5cc6a87d23f7065a16e858
1 /****************************************
2 * File: netapi_sched.c
3 * Purpose: netapi scheduling module
4 * NOTE: This sample right now.
5 **************************************************************
6 * FILE: netapi_sched.c
7 *
8 * DESCRIPTION: netapi sample scheduler source file for user space transport
9 * library
10 *
11 * REVISION HISTORY: rev 0.0.1
12 *
13 * Copyright (c) Texas Instruments Incorporated 2013
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the
25 * distribution.
26 *
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 * its contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 ****************************************/
45 #include "netapi_sched.h"
47 #define NO_TIMER //turn off timer related scheduling
50 /********************************************************************
51 * FUNCTION PURPOSE: API to get scheduling context statistics
52 ********************************************************************
53 * DESCRIPTION: API to get scheduling context statistics
54 ********************************************************************/
55 void netapi_schedGetStats(NETAPI_SCHED_HANDLE_T *s,
56 unsigned long long * p_pkts,
57 unsigned long long * p_cycles,
58 unsigned long long * p_cache_cycles)
59 {
60 *p_pkts= s->stats.num_pkts;
61 *p_cycles= s->stats.busy_cycles;
62 *p_cache_cycles= s->stats.cache_cycles;
63 return;
64 }
65 /****************************************/
66 /************API************************/
67 /**************************************/
69 /********************************************************************
70 * FUNCTION PURPOSE: API to open a scheduling context
71 ********************************************************************
72 * DESCRIPTION: API to open a scheduling context
73 ********************************************************************/
74 NETAPI_SCHED_HANDLE_T * netapi_schedOpen(NETAPI_T n,
75 NETAPI_SCHED_CONFIG_T * p_config,
76 int *p_err)
77 {
78 *p_err=0;
79 NETAPI_SCHED_HANDLE_T * ph = (NETAPI_SCHED_HANDLE_T *) netapi_get_scheduler(n);
80 if(!ph) {*p_err= NETAPI_ERR_NOMEM; return NULL;}
81 if(!p_config) {*p_err= NETAPI_ERR_BAD_INPUT; return NULL;}
82 memcpy(&ph->config,p_config,sizeof(NETAPI_SCHED_CONFIG_T));
83 ph->start = hplib_mUtilGetTimestamp();
84 ph->back = (void *) n;
85 if (ph->config.valid_flags & NETAPI_SCHED_DURATION)
86 {
87 if (ph->config.duration == NETAPI_SCHED_FOREVER)
88 {
89 ph->shutdown_time=(uint64_t) -1;
90 }
91 else
92 {
93 ph->shutdown_time = ph->start + ph->config.duration;
94 }
95 }
96 else ph->shutdown_time = (uint64_t) -1;
97 ph->state =NETAPI_SCHED_STATE_OPEN;
98 return(ph);
99 }
101 /********************************************************************
102 * FUNCTION PURPOSE: API to re-configure a scheduling context, FUTURE,
103 * not implemented
104 ********************************************************************
105 * DESCRIPTION: API to re-configure a scheduling context, FUTURE,
106 * not implemented
107 ********************************************************************/
109 int netapi_schedControl(NETAPI_SCHED_HANDLE_T *s,
110 NETAPI_SCHED_CONFIG_T *p_config,
111 int *p_err)
112 {
113 /* NOT_IMPLEMENTED */
114 return 0;
115 }
117 /********************************************************************
118 * FUNCTION PURPOSE: API for main entry point to scheduler
119 ********************************************************************
120 * DESCRIPTION: API to get NETAPI scheduling context statistics
121 ********************************************************************/
122 /* main entry point. caller gives up control to scheduler */
123 int netapi_schedRun(NETAPI_SCHED_HANDLE_T *s, int *p_err)
124 {
125 int err;
126 *p_err=0;
127 unsigned long long t = hplib_mUtilGetTimestamp();
128 int next_house;
129 volatile int pkts;
130 volatile unsigned long t1;
131 volatile unsigned long t2;
132 volatile unsigned long long cache_op_b2;
133 volatile unsigned long long n_c_ops;
135 next_house = s->config.interval;
136 /* loop for duration or until shutdown */
137 for(;t< s->shutdown_time;)
138 {
139 #ifndef NO_TIMER
140 t = hplib_mUtilGetTimestamp();
141 #endif
142 next_house -=1;
144 cache_op_b2= Osal_cache_op_measure(&n_c_ops);
145 t1=hplib_mUtilGetPmuCCNT();
146 //poll all pktio channels we have open in RX mode
147 pkts=pktio_pollAll((NETAPI_T) s->back, NULL, &err);
148 if (!pkts && (s->config.yield == TRUE))
149 {
150 sched_yield();
151 }
152 t2=hplib_mUtilGetPmuCCNT();
153 if (pkts)
154 {
155 s->stats.num_pkts+= (unsigned long long) pkts;
156 s->stats.busy_cycles += (unsigned long long) (t2-t1);
157 cache_op_b2= Osal_cache_op_measure(&n_c_ops)- cache_op_b2;
158 s->stats.cache_cycles += (unsigned long long) cache_op_b2;
159 }
161 //poll pktlib garbage collections for registered heaps
162 if (TRUE == s->config.pollGarbageQ)
163 {
164 netapi_pollHeapGarbage((NETAPI_T) s->back);
165 }
168 //poll NETCP/PA control channels
169 if (TRUE == s->config.pollCtrlQ)
170 {
171 netapi_netcpPoll((NETAPI_T) s->back);
172 }
174 //see if time to do a house keeping callback
175 if ((s->config.valid_flags & NETAPI_SCHED_CBV) && s->config.house_cb)
176 if (next_house<=0)
177 {
178 s->config.house_cb(s);
179 next_house = s->config.interval;
180 }
181 //see if we were closed and/or its time to close
182 if (s->state!= NETAPI_SCHED_STATE_OPEN)
183 {
184 s->state=NETAPI_SCHED_STATE_CLOSE;
185 break;
186 }
188 }
189 return 1;
190 }
191 /********************************************************************
192 * FUNCTION PURPOSE: API to close a scheduling context
193 ********************************************************************
194 * DESCRIPTION: API to close a scheduling context
195 ********************************************************************/
196 /* shutdown scheduler context */
197 int netapi_schedClose(NETAPI_SCHED_HANDLE_T * s, NETAPI_SCHED_SHUTDOWN_T * p_close, int * p_err)
198 {
199 *p_err=0;
200 if (p_close->shutdown_type == NETAPI_SCHED_SHUTDOWN_NOW)
201 {
202 s->state=NETAPI_SCHED_STATE_CLOSE_IN_PROGRESS; //say we are closing
203 }
204 return 1;
205 }