Skip to content

Commit

Permalink
rtems: Update to RTEMS 6 with Libbsd and Legacy net support
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwichris committed Aug 25, 2024
1 parent 7f90d16 commit 1a65c02
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 18 deletions.
38 changes: 34 additions & 4 deletions devIocStats/os/RTEMS/devIocStatsOSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,21 @@
* info of 100% free (but 100% of 0 is still 0).
*
*/
/*
* Updated to RTEMS 6
* Contemporary Software
* Chris Johns <[email protected]>
*/
#include "epicsVersion.h"

#define RTEMS_VERSION_INT \
VERSION_INT(__RTEMS_MAJOR__, __RTEMS_MINOR__, __RTEMS_REVISION__, 0)

#if RTEMS_VERSION_INT <= VERSION_INT(5, 0, 0, 0)
/*
* This define effects rtems.h includes on 4.10 and earlier
*/
#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
#include <rtems.h>
#include <bsp.h>
#include <rtems/libcsupport.h>
#include <rtems/libio_.h>
Expand All @@ -53,6 +66,9 @@
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#endif /* RTEMS 5 and earlier */

#include <rtems.h>

#undef malloc
#undef free
Expand All @@ -67,17 +83,30 @@
#include <string.h>
#include <stdlib.h>

#include "epicsVersion.h"
#if RTEMS_LEGACY_STACK

#define RTEMS_VERSION_INT \
VERSION_INT(__RTEMS_MAJOR__, __RTEMS_MINOR__, __RTEMS_REVISION__, 0)
#include <rtems/rtems_bsdnet.h>

#define sysBootLine rtems_bsdnet_bootp_cmdline
#else
#define sysBootLine "BOOTP cmdline not supported"
#endif
/* Override default STARTUP environment variable to use INIT */
#undef STARTUP
#define STARTUP "INIT"

#define CLUSTSIZES 2 /* only regular mbufs and clusters */

#if RTEMS_VERSION_INT >= VERSION_INT(6, 0, 0, 0)

#define NO_OF_CPUS rtems_configuration_get_maximum_processors()

#include <bsp/bootcard.h>
static inline void reboot(int val) {
(void) val;
bsp_reset();
}
#else /* RTEMS 6 or later */
#ifdef RTEMS_BSP_PGM_EXEC_AFTER /* only defined on uC5282 */
#define reboot(x) bsp_reset(0)
#elif (defined(__PPC__) && RTEMS_VERSION_INT > VERSION_INT(4, 9, 0, 0))
Expand All @@ -91,3 +120,4 @@
#else
#define reboot(x) rtemsReboot()
#endif
#endif /* RTEMS 6 or later */
60 changes: 58 additions & 2 deletions devIocStats/os/RTEMS/osdClustInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,65 @@

#include <devIocStats.h>

/* This would otherwise need _KERNEL to be defined... */
extern struct mbstat mbstat;
#include <sys/param.h>
#include <sys/mbuf.h>

int devIocStatsInitClusterInfo(void) { return 0; }

#if RTEMS_LIBBSD_STACK

#include <memstat.h>

int devIocStatsGetClusterInfo(int pool, clustInfo *pval) {
struct memory_type_list *mtlp;
struct memory_type *mtp;

if (pool == DATA_POOL) {
return -1;
}

mtlp = memstat_mtl_alloc();
if (mtlp == NULL) {
return -1;
}

if (memstat_sysctl_all(mtlp, 0) < 0) {
memstat_mtl_free(mtlp);
return -1;
}

mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
if (mtp == NULL) {
memstat_mtl_free(mtlp);
return -1;
}

(*pval)[0][0] = memstat_get_size(mtp);
(*pval)[0][1] = memstat_get_count(mtp);
(*pval)[0][2] = memstat_get_free(mtp);
(*pval)[0][3] = (*pval)[0][1] - (*pval)[0][2];

mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
if (mtp == NULL) {
memstat_mtl_free(mtlp);
return -1;
}

(*pval)[0][0] = memstat_get_size(mtp);
(*pval)[0][1] = memstat_get_count(mtp);
(*pval)[0][2] = memstat_get_free(mtp);
(*pval)[0][3] = (*pval)[0][1] - (*pval)[0][2];

memstat_mtl_free(mtlp);

return 0;
}

#else /* RTEMS_LIBBSD_STACK */

/* This would otherwise need _KERNEL to be defined... */
extern struct mbstat mbstat;

int devIocStatsGetClusterInfo(int pool, clustInfo *pval) {
if (pool == DATA_POOL)
return -1;
Expand All @@ -86,3 +140,5 @@ int devIocStatsGetClusterUsage(int pool, int *pval) {

return 0;
}

#endif /* RTEMS_LIBBSD_STACK */
37 changes: 34 additions & 3 deletions devIocStats/os/RTEMS/osdCpuUsage.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@

#include <devIocStats.h>

static double prev_total = 0;
static double prev_idle = 0;

#if __RTEMS_MAJOR__ >= 6

#include <sys/resource.h>

static double oldActiveUsage;
static double oldIdleUsage;

/*
* See IEEE Std 1003.1-1988 (“POSIX.1”) for getrusage()
*/
static void cpu_ticks(double *total, double *idle) {
struct rusage stats;
double curActive;
double curIdle;
getrusage(RUSAGE_SELF, &stats);
curActive = (double) stats.ru_utime.tv_sec + stats.ru_utime.tv_usec / 1e6;
curIdle = (double) stats.ru_stime.tv_sec + stats.ru_stime.tv_usec / 1e6;
*idle = curIdle - oldIdleUsage;
*total = *idle + (curActive - oldActiveUsage);
oldActiveUsage = curActive;
oldIdleUsage = curIdle;
}

#else /* RTEMS 6 or later */

/*
* Direct access to the Object information table
*/

#if (__RTEMS_MAJOR__ > 4) || (__RTEMS_MAJOR__ == 4 && __RTEMS_MINOR__ > 7)
typedef char objName[13];
#define RTEMS_OBJ_GET_NAME(tc, name) \
Expand Down Expand Up @@ -79,9 +111,6 @@ typedef char *objName;
* from the RTEMS source.
*/

static double prev_total = 0;
static double prev_idle = 0;

static void cpu_ticks(double *total, double *idle) {
Objects_Information *obj;
Thread_Control *tc;
Expand Down Expand Up @@ -116,6 +145,8 @@ static void cpu_ticks(double *total, double *idle) {
}
}

#endif /* RTEMS 6 or later */

int devIocStatsInitCpuUsage(void) {
cpu_ticks(&prev_total, &prev_idle);
#ifdef SSRLAPPSMISCUTILS
Expand Down
1 change: 0 additions & 1 deletion devIocStats/os/RTEMS/osdCpuUtilization.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ int devIocStatsInitCpuUtilization(loadInfo *pval) {
return 0;
}

/* FIXME: This relies on the device support calling it after CpuUsage */
int devIocStatsGetCpuUtilization(loadInfo *pval) {
pval->iocLoad = pval->cpuLoad;
return 0;
Expand Down
8 changes: 8 additions & 0 deletions devIocStats/os/RTEMS/osdFdUsage.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@

#include <devIocStats.h>

#if __RTEMS_MAJOR__ >= 6
#include <rtems/libio_.h>
#endif /* _RTEMS_MAJOR__ >= 6 */

int devIocStatsInitFDUsage(void) { return 0; }

int devIocStatsGetFDUsage(fdInfo *pval) {
#if __RTEMS_MAJOR__ >= 6
pval->used = rtems_libio_count_open_iops();
#else
int i, tot;

for (tot = 0, i = 0; i < rtems_libio_number_iops; i++) {
if (rtems_libio_iops[i].flags & LIBIO_FLAGS_OPEN)
tot++;
}
pval->used = tot;
#endif /* _RTEMS_MAJOR__ >= 6 */
pval->max = rtems_libio_number_iops;
return 0;
}
41 changes: 39 additions & 2 deletions devIocStats/os/RTEMS/osdIFErrors.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,47 @@

#include <devIocStats.h>

int devIocStatsInitIFErrors(void) { return 0; }

#if RTEMS_LIBBSD_STACK

#include <ifaddrs.h>
#include <net/if.h>

int devIocStatsGetIFErrors(ifErrInfo *pval) {
struct ifaddrs *ifap, *ifa;

/* add all interfaces' errors */
pval->ierrors = 0;
pval->oerrors = 0;

if (getifaddrs(&ifap) != 0) {
return -1;
}

for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family != AF_LINK) {
continue;
}
#define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
pval->ierrors += IFA_STAT(ierrors);
pval->oerrors += IFA_STAT(oerrors);
}

freeifaddrs(ifap);

return 0;
}

#else /* RTEMS_LIBBSD_STACK */

#include <net/if_var.h>

/* This would otherwise need _KERNEL to be defined... */
extern struct ifnet *ifnet;

int devIocStatsInitIFErrors(void) { return 0; }

int devIocStatsGetIFErrors(ifErrInfo *pval) {

struct ifnet *ifp;

/* add all interfaces' errors */
Expand All @@ -57,3 +92,5 @@ int devIocStatsGetIFErrors(ifErrInfo *pval) {
}
return 0;
}

#endif /* RTEMS_LIBBSD_STACK */
4 changes: 4 additions & 0 deletions devIocStats/os/RTEMS/osdSuspTasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
int devIocStatsInitSuspTasks(void) { return 0; }

int devIocStatsGetSuspTasks(int *pval) {
#if __RTEMS_MAJOR__ < 6
Objects_Control *o;
Objects_Id id = OBJECTS_ID_INITIAL_INDEX;
Objects_Id nid;
Expand All @@ -59,4 +60,7 @@ int devIocStatsGetSuspTasks(int *pval) {
}
*pval = n;
return 0;
#else /* __RTEMS_MAJOR__ < 6 */
return -1;
#endif /* __RTEMS_MAJOR__ < 6 */
}
17 changes: 11 additions & 6 deletions devIocStats/os/RTEMS/osdWorkspaceUsage.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ int devIocStatsInitWorkspaceUsage(void) { return 0; }

int devIocStatsGetWorkspaceUsage(memInfo *pval) {
Heap_Information_block info;
#ifdef RTEMS_PROTECTED_HEAP
#if __RTEMS_MAJOR__ >= 6
malloc_info(&info);
pval->numBytesTotal = info.Stats.size;
#else /* __RTEMS_MAJOR__ >= 6 */
#ifdef RTEMS_PROTECTED_HEAP
_Protected_heap_Get_information(&_Workspace_Area, &info);
#else /* RTEMS_PROTECTED_HEAP */
#else /* RTEMS_PROTECTED_HEAP */
_RTEMS_Lock_allocator(); /*Lock allocator to ensure accuracy */
_Heap_Get_information(
&_Workspace_Area,
&info); /*_Heap_Get_information is part of the RTEMS API */
_RTEMS_Unlock_allocator();
#endif /* RTEMS_PROTECTED_HEAP */
#if (__RTEMS_MAJOR__ > 4) || (__RTEMS_MAJOR__ == 4 && __RTEMS_MINOR__ > 9)
#endif /* RTEMS_PROTECTED_HEAP */
#if (__RTEMS_MAJOR__ > 4) || (__RTEMS_MAJOR__ == 4 && __RTEMS_MINOR__ > 9)
pval->numBytesTotal = Configuration.work_space_size;
#else
#else
pval->numBytesTotal = _Configuration_Table->work_space_size;
#endif
#endif
#endif /* _RTEMS_MAJOR__ >= 6 */
pval->numBytesFree = info.Free.total;
pval->numBytesAlloc = info.Used.total;
return 0;
Expand Down

0 comments on commit 1a65c02

Please sign in to comment.