summaryrefslogtreecommitdiffstats
path: root/posix_api/psx_pthread_report/pthread_attr_report.c
blob: ff2f1799107f61f1981c090d67410926361e9221 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 *  Program to print default pthread attributes
 */

/*
 * Copyright 2018 Joel Sherrill (joel@rtems.org)
 *
 * This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
 */

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <pthread.h>
#include <assert.h>

static void print_stack_info(pthread_attr_t *attr)
{
  int      rc;
  void    *addr;
  size_t   size;

  /*
   * pthread_attr_getstacksize() and pthread_attr_[sg]etstackaddr() are
   * obsolete.
   */

  rc = pthread_attr_getstack( attr, &addr, &size );
  assert( rc == 0 );
  printf( "Stack size: %zu\n", size );
  printf( "Stack address: %p\n", addr );

  rc = pthread_attr_getguardsize( attr, &size );
  assert( rc == 0 );
  printf( "Stack guard size: %zu\n", size );

}

static void print_inherit_scheduler(pthread_attr_t *attr)
{
  int      rc;
  int      sched;
  char    *s;

  rc = pthread_attr_getinheritsched( attr, &sched );
  assert( rc == 0 );

  switch( sched ) {
    case PTHREAD_INHERIT_SCHED:  s = "PTHREAD_INHERIT_SCHED";  break;
    case PTHREAD_EXPLICIT_SCHED: s = "PTHREAD_EXPLICIT_SCHED"; break;
    default:                     s = "UNKNOWN"; break;
  }

  printf( "Inherit scheduler: %s\n", s );
}

static void print_scope(pthread_attr_t *attr)
{
  int      rc;
  int      scope;
  char    *s;

  rc = pthread_attr_getscope( attr, &scope );
  assert( rc == 0 );

  switch( scope ) {
    case PTHREAD_SCOPE_SYSTEM:  s = "PTHREAD_SCOPE_SYSTEM";  break;
    case PTHREAD_SCOPE_PROCESS: s = "PTHREAD_SCOPE_PROCESS"; break;
    default:                     s = "UNKNOWN"; break;
  }

  printf( "Thread scope: %s\n", s );
}

static void print_detach_state(pthread_attr_t *attr)
{
  int      rc;
  int      state;
  char    *s;

  rc = pthread_attr_getdetachstate( attr, &state );
  assert( rc == 0 );

  switch( state ) {
    case PTHREAD_CREATE_JOINABLE: s = "PTHREAD_CREATE_JOINABLE";  break;
    case PTHREAD_CREATE_DETACHED: s = "PTHREAD_CREATE_DETACHED"; break;
    default:                      s = "UNKNOWN"; break;
  } 

  printf( "Thread detach state: %s\n", s );
}

static void print_sched_info(pthread_attr_t *attr)
{
  int                 rc;
  int                 policy;
  char               *s;
  struct sched_param  sched;

  rc = pthread_attr_getschedpolicy( attr, &policy );
  assert( rc == 0 );

  rc = pthread_attr_getschedparam( attr, &sched );
  assert( rc == 0 );

  switch( policy ) {
    case SCHED_OTHER:    s = "SCHED_OTHER";    break;
    case SCHED_FIFO:     s = "SCHED_FIFO";     break;
    case SCHED_RR:       s = "SCHED_RR";       break;
#if defined(SCHED_SPORADIC)
    case SCHED_SPORADIC: s = "SCHED_SPORADIC"; break;
#endif
    default:             s = "UNKNOWN";        break;
  } 

  printf( "Scheduler policy : %s\n", s );
  printf( "Scheduler priority : %d\n", sched.sched_priority );
#if defined(SCHED_SPORADIC)
  if ( policy == SCHED_SPORADIC ) {
    printf(
      "Sporadic low priority : %d\n"
      "Sporadic period: %lld:%ld\n"
      "Sporadic initial budget: %lld:%ld\n"
      "Sporadic max pending replenishments: %d\n",
      sched.sched_ss_low_priority,
      (long long) sched.sched_ss_repl_period.tv_sec,
      sched.sched_ss_repl_period.tv_nsec,
      (long long) sched.sched_ss_init_budget.tv_sec,
      sched.sched_ss_init_budget.tv_nsec,
      sched.sched_ss_max_repl
    );
  }
#endif
}

int main()
{
  pthread_attr_t  attr;
  int             rc;
  
  puts( "*** POSIX Thread Default Attributes Report ***" );

  rc = pthread_attr_init( &attr );
  assert( rc == 0 );

  print_stack_info( &attr );
  print_inherit_scheduler( &attr );
  print_scope( &attr );
  print_detach_state( &attr );
  print_sched_info( &attr );

  puts( "*** END OF POSIX Thread Default Attributes Report ***" );
  exit( 0 );
}