Freeciv21
Develop your civilization from humble roots to a global empire
srv_log.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // utility
15 #include "shared.h"
16 #include "timing.h"
17 
18 // common
19 #include "ai.h"
20 #include "city.h"
21 #include "game.h"
22 #include "map.h"
23 #include "nation.h"
24 #include "unit.h"
25 
26 // server
27 #include "notify.h"
28 
29 /* server/advisors */
30 #include "advdata.h"
31 
32 #include "srv_log.h"
33 
35 static int recursion[AIT_LAST];
36 
37 // General AI logging functions
38 
43 QString city_log_prefix(const city *pcity)
44 {
45  char aibuf[500] = "\0";
46  CALL_PLR_AI_FUNC(log_fragment_city, city_owner(pcity), aibuf,
47  sizeof(aibuf), pcity);
48 
49  return QString::asprintf("%s %s(%d,%d) (s%d) {%s} ",
51  city_name_get(pcity), TILE_XY(pcity->tile),
52  city_size_get(pcity), aibuf);
53 }
54 
61 QString unit_log_prefix(const unit *punit)
62 {
63  int gx, gy;
64  char aibuf[500] = "\0";
65 
66  CALL_PLR_AI_FUNC(log_fragment_unit, unit_owner(punit), aibuf,
67  sizeof(aibuf), punit);
68 
69  if (punit->goto_tile) {
70  index_to_map_pos(&gx, &gy, tile_index(punit->goto_tile));
71  } else {
72  gx = gy = -1;
73  }
74 
75  return QString::asprintf("%s %s(%d) %s (%d,%d)->(%d,%d){%s} ",
77  unit_rule_name(punit), punit->id,
79  TILE_XY(unit_tile(punit)), gx, gy, aibuf);
80 }
81 
86 void timing_log_real(enum ai_timer timer, enum ai_timer_activity activity)
87 {
88  static int turn = -1;
89 
90  if (game.info.turn != turn) {
91  int i;
92 
93  turn = game.info.turn;
94  for (i = 0; i < AIT_LAST; i++) {
95  timer_clear(aitimer[i][0]);
96  }
97  fc_assert(activity == TIMER_START);
98  }
99 
100  if (activity == TIMER_START && recursion[timer] == 0) {
101  timer_start(aitimer[timer][0]);
102  timer_start(aitimer[timer][1]);
103  recursion[timer]++;
104  } else if (activity == TIMER_STOP && recursion[timer] == 1) {
105  timer_stop(aitimer[timer][0]);
106  timer_stop(aitimer[timer][1]);
107  recursion[timer]--;
108  }
109 }
110 
115 {
116  char buf[200];
117 
118 #define AILOG_OUT(text, which) \
119  fc_snprintf(buf, sizeof(buf), " %s: %g sec turn, %g sec game", text, \
120  timer_read_seconds(aitimer[which][0]), \
121  timer_read_seconds(aitimer[which][1])); \
122  qCInfo(timers_category, "%s", buf); \
123  notify_conn(nullptr, nullptr, E_AI_DEBUG, ftc_log, "%s", buf);
124 
125  qCInfo(timers_category, " --- AI timing results ---");
126 
127  notify_conn(nullptr, nullptr, E_AI_DEBUG, ftc_log,
128  " --- AI timing results ---");
129  AILOG_OUT("Total AI time", AIT_ALL);
130  AILOG_OUT("Movemap", AIT_MOVEMAP);
131  AILOG_OUT("Units", AIT_UNITS);
132  AILOG_OUT(" - Military", AIT_MILITARY);
133  AILOG_OUT(" - Attack", AIT_ATTACK);
134  AILOG_OUT(" - Defense", AIT_DEFENDERS);
135  AILOG_OUT(" - Ferry", AIT_FERRY);
136  AILOG_OUT(" - Rampage", AIT_RAMPAGE);
137  AILOG_OUT(" - Bodyguard", AIT_BODYGUARD);
138  AILOG_OUT(" - Recover", AIT_RECOVER);
139  AILOG_OUT(" - Caravan", AIT_CARAVAN);
140  AILOG_OUT(" - Hunter", AIT_HUNTER);
141  AILOG_OUT(" - Airlift", AIT_AIRLIFT);
142  AILOG_OUT(" - Diplomat", AIT_DIPLOMAT);
143  AILOG_OUT(" - Air", AIT_AIRUNIT);
144  AILOG_OUT(" - Explore", AIT_EXPLORER);
145  AILOG_OUT("fstk", AIT_FSTK);
146  AILOG_OUT("Settlers", AIT_SETTLERS);
147  AILOG_OUT("Workers", AIT_WORKERS);
148  AILOG_OUT("Government", AIT_GOVERNMENT);
149  AILOG_OUT("Taxes", AIT_TAXES);
150  AILOG_OUT("Cities", AIT_CITIES);
151  AILOG_OUT(" - Buildings", AIT_BUILDINGS);
152  AILOG_OUT(" - Danger", AIT_DANGER);
153  AILOG_OUT(" - Worker want", AIT_CITY_TERRAIN);
154  AILOG_OUT(" - Military want", AIT_CITY_MILITARY);
155  AILOG_OUT(" - Settler want", AIT_CITY_SETTLERS);
156  AILOG_OUT("Citizen arrange", AIT_CITIZEN_ARRANGE);
157  AILOG_OUT("Tech", AIT_TECH);
158 }
159 
164 {
165  int i;
166 
167  for (i = 0; i < AIT_LAST; i++) {
170  recursion[i] = 0;
171  }
172 }
173 
178 {
179  int i;
180 
181  for (i = 0; i < AIT_LAST; i++) {
182  timer_destroy(aitimer[i][0]);
183  timer_destroy(aitimer[i][1]);
184  }
185 }
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition: ai.h:383
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
const struct ft_color ftc_log
struct civ_game game
Definition: game.cpp:47
#define fc_assert(condition)
Definition: log.h:89
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
struct nation_type * nation_of_city(const struct city *pcity)
Return the nation of the player who owns the city.
Definition: nation.cpp:429
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
struct nation_type * nation_of_unit(const struct unit *punit)
Return the nation of the player who owns the unit.
Definition: nation.cpp:438
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
See notify_conn_packet - this is just the "non-v" version, with varargs.
Definition: notify.cpp:235
void timing_log_init()
Initialize AI timing system.
Definition: srv_log.cpp:163
void timing_log_free()
Free AI timing system resources.
Definition: srv_log.cpp:177
QString unit_log_prefix(const unit *punit)
Log a unit, it will appear like this Polish Archers[139] (5,35)->(0,0){0,0} where [] is unit id,...
Definition: srv_log.cpp:61
QString city_log_prefix(const city *pcity)
Log a city, it will appear like this Polish Romenna(5,35) [s1 d106 u11 g1].
Definition: srv_log.cpp:43
static int recursion[AIT_LAST]
Definition: srv_log.cpp:35
#define AILOG_OUT(text, which)
void timing_log_real(enum ai_timer timer, enum ai_timer_activity activity)
Measure the time between the calls.
Definition: srv_log.cpp:86
static civtimer * aitimer[AIT_LAST][2]
Definition: srv_log.cpp:34
void timing_results_real()
Print results.
Definition: srv_log.cpp:114
ai_timer
Definition: srv_log.h:42
@ AIT_RECOVER
Definition: srv_log.h:70
@ AIT_FSTK
Definition: srv_log.h:56
@ AIT_GOVERNMENT
Definition: srv_log.h:49
@ AIT_CARAVAN
Definition: srv_log.h:58
@ AIT_RAMPAGE
Definition: srv_log.h:73
@ AIT_CITY_MILITARY
Definition: srv_log.h:65
@ AIT_BUILDINGS
Definition: srv_log.h:53
@ AIT_TAXES
Definition: srv_log.h:50
@ AIT_ATTACK
Definition: srv_log.h:68
@ AIT_HUNTER
Definition: srv_log.h:59
@ AIT_BODYGUARD
Definition: srv_log.h:71
@ AIT_DEFENDERS
Definition: srv_log.h:57
@ AIT_SETTLERS
Definition: srv_log.h:46
@ AIT_UNITS
Definition: srv_log.h:45
@ AIT_CITIES
Definition: srv_log.h:51
@ AIT_AIRUNIT
Definition: srv_log.h:62
@ AIT_EXPLORER
Definition: srv_log.h:63
@ AIT_TECH
Definition: srv_log.h:55
@ AIT_CITY_TERRAIN
Definition: srv_log.h:66
@ AIT_FERRY
Definition: srv_log.h:72
@ AIT_MILITARY
Definition: srv_log.h:69
@ AIT_ALL
Definition: srv_log.h:43
@ AIT_MOVEMAP
Definition: srv_log.h:44
@ AIT_WORKERS
Definition: srv_log.h:47
@ AIT_DIPLOMAT
Definition: srv_log.h:61
@ AIT_LAST
Definition: srv_log.h:74
@ AIT_AIRLIFT
Definition: srv_log.h:60
@ AIT_CITIZEN_ARRANGE
Definition: srv_log.h:52
@ AIT_CITY_SETTLERS
Definition: srv_log.h:67
@ AIT_DANGER
Definition: srv_log.h:54
ai_timer_activity
Definition: srv_log.h:77
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
Definition: city.h:291
struct tile * tile
Definition: city.h:293
struct packet_game_info info
Definition: game.h:80
Definition: unit.h:134
enum unit_activity activity
Definition: unit.h:154
int id
Definition: unit.h:141
struct tile * goto_tile
Definition: unit.h:152
#define tile_index(_pt_)
Definition: tile.h:70
#define TILE_XY(ptile)
Definition: tile.h:36
void timer_destroy(civtimer *t)
Deletes timer.
Definition: timing.cpp:66
civtimer * timer_new(enum timer_timetype type, enum timer_use use)
Allocate a new timer with specified "type" and "use".
Definition: timing.cpp:43
void timer_start(civtimer *t)
Start timing, adding to previous accumulated time if timer has not been cleared.
Definition: timing.cpp:95
void timer_clear(civtimer *t)
Reset accumulated time to zero, and stop timer if going.
Definition: timing.cpp:83
void timer_stop(civtimer *t)
Stop timing, and accumulate time so far.
Definition: timing.cpp:116
@ TIMER_ACTIVE
Definition: timing.h:25
@ TIMER_CPU
Definition: timing.h:20
const char * get_activity_text(enum unit_activity activity)
Return the name of the activity in a static buffer.
Definition: unit.cpp:586
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283