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 2010-2011
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.h"
46 #include "netapi_sched.h"
48 #define NO_TIMER //turn off timer related scheduling
49 #define NO_GARBAGE //turn off garbage collection
51 void netapi_sched_get_stats(NETAPI_SCHED_HANDLE_T *s,
52 unsigned long long * p_pkts,
53 unsigned long long * p_cycles,
54 unsigned long long * p_cache_cycles)
55 {
56 *p_pkts= s->num_pkts;
57 *p_cycles= s->busy_cycles;
58 *p_cache_cycles= s->cache_cycles;
59 return;
60 }
61 /****************************************/
62 /************API************************/
63 /**************************************/
65 /* open a scheduling contex */
66 NETAPI_SCHED_HANDLE_T * netapi_schedOpen(NETAPI_T n, NETAPI_SCHED_CONFIG_T * p_config, int *p_err)
67 {
68 *p_err=0;
69 NETAPI_SCHED_HANDLE_T * ph = (NETAPI_SCHED_HANDLE_T *) netapi_get_scheduler(n);
70 if(!ph) {*p_err= NETAPI_ERR_NOMEM; return NULL;}
71 if(!p_config) {*p_err= NETAPI_ERR_BAD_INPUT; return NULL;}
72 memcpy(&ph->config,p_config,sizeof(NETAPI_SCHED_CONFIG_T));
73 ph->start = hplib_mUtil_GetTimestamp();
74 ph->back = (void *) n;
75 if (ph->config.valid_flags & NETAPI_SCHED_DURATION)
76 {
77 if (ph->config.duration == NETAPI_SCHED_FOREVER)
78 {
79 ph->shutdown_time=(uint64_t) -1;
80 }
81 else
82 {
83 ph->shutdown_time = ph->start + ph->config.duration;
84 }
85 }
86 else ph->shutdown_time = (uint64_t) -1;
87 ph->state =NETAPI_SCHED_STATE_OPEN;
88 return(ph);
90 }
92 /* re-configure a scheduling context */
93 int netapi_schedControl(NETAPI_SCHED_HANDLE_T *s, NETAPI_SCHED_CONFIG_T *p_config, int *p_err)
94 {
95 /* not implemented */
97 return 0;
98 }
101 /* main entry point. caller gives up control to scheduler */
102 int netapi_schedWaitForEvents(NETAPI_SCHED_HANDLE_T *s, int *p_err)
103 {
104 int err;
105 *p_err=0;
106 unsigned long long t = hplib_mUtil_GetTimestamp();
107 int next_house;
108 volatile int pkts;
109 volatile unsigned long t1;
110 volatile unsigned long t2;
111 volatile unsigned long long cache_op_b2;
112 volatile unsigned long long n_c_ops;
114 next_house = s->config.interval;
115 /* loop for duration or until shutdown */
116 for(;t< s->shutdown_time;)
117 {
118 #ifndef NO_TIMER
119 t = hplib_mUtil_GetTimestamp();
120 #endif
121 next_house -=1;
122 //poll all pktio channels we have open in RX mode
123 //Osal_cache_op_measure_reset();
124 cache_op_b2= Osal_cache_op_measure(&n_c_ops);
125 t1=hplib_mUtil_GetTickCount();
126 pkts=pktio_pollAll((NETAPI_T) s->back, NULL, &err);
127 t2=hplib_mUtil_GetTickCount();
128 if (pkts)
129 {
130 s->num_pkts+= (unsigned long long) pkts;
131 s->busy_cycles += (unsigned long long) (t2-t1);
132 cache_op_b2= Osal_cache_op_measure(&n_c_ops)- cache_op_b2;
133 s->cache_cycles += (unsigned long long) cache_op_b2;
134 }
136 #ifndef NO_GARBAGE
137 //poll pktlib garbage collections for registered heaps..
138 netapi_pollHeapGarbage((NETAPI_T) s->back);
139 #endif
141 #ifndef NO_TIMER
142 //todo timers (local and global)
143 hplib_TimerPollAllGroups((NETAPI_T) s->back, HPLIB_TIMER_FITLER_ALL, 100000);
144 #endif
145 //poll NETCP/PA control channels
146 netapi_netcpPoll((NETAPI_T) s->back);
148 //see if time to do a house keeping callback
149 if ((s->config.valid_flags & NETAPI_SCHED_CBV) && s->config.house_cb)
150 if (next_house<=0)
151 {
152 s->config.house_cb(s);
153 next_house = s->config.interval;
154 }
155 //see if we were closed and/or its time to close
156 if (s->state!= NETAPI_SCHED_STATE_OPEN)
157 { s->state=NETAPI_SCHED_STATE_CLOSE; break;}
158 }
159 return 1;
160 }
162 /* shutdown scheduler context */
163 int netapi_schedShutdown(NETAPI_SCHED_HANDLE_T * s, NETAPI_SCHED_SHUTDOWN_T * p_close, int * p_err)
164 {
165 *p_err=0;
166 s->state=NETAPI_SCHED_STATE_CLOSE_IN_PROGRESS; //say we are closing
168 return 1;
169 }